MySQL data does not appear in tooltip - php

I want to show MySQL data inside a tooltip.
Here is the code:
<?php
$abfrage = "SELECT * FROM tester";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis)){
$Name=$row->Name;
$Bech=$row->Beschreibung;
}
?>
Test Link
It shows me an empty tooltip with nothing inside. If I print those two variables, the MySQL data appear. Is there any mistake in the code?
Thanks.

This is in conjunction with Seth McClaine's answer.
Echo your values using:
<?php echo $Name; ?> <?php echo $Bech; ?>
Instead of <?=$Name?> <?=$Bech?>
The use of short tags is not recommended for something like this.
Reformatted code:
<?php
$abfrage = "SELECT * FROM tester";
$ergebnis = mysql_query($abfrage);
$row = mysql_fetch_object($ergebnis);
$Name=$row->Name;
$Bech=$row->Beschreibung;
?>
Test Link

If you just want the first result remove the while...
<?php
$abfrage = "SELECT * FROM tester";
$ergebnis = mysql_query($abfrage);
$row = mysql_fetch_object($ergebnis);
$Name=$row->Name;
$Bech=$row->Beschreibung;
?>
Test Link

Related

How can I add an Sql Query in the middle of HTML code?

I want to make a login system, where the user can see all the messages he has received. In my database the insurance_company is the username and that is what I want it to show by. Here is my code. How do I fix it?
- Thanks Prem
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Claim'] . "<br />";
}
?>
PHP code can be written anywhere in HTML code. You need to just place your PHP code between tags. As you can see in the example bellow I have used php code between p tags.
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {?>
<p> <?php echo $row['Claim'] . "<br />"; ?> <p>
<?php }
?>
If you want it to show insurance_company, you must echo this value in the response array:
echo $row['insurance_company']

php error - Undefined index: id

I have got an error message in php like this "undefined index id in line no:", I have using the following code
$id='$_REQUEST[id]';
$sql = "SELECT * FROM country";
$result = $con->query($sql);
$i=1;
foreach($result as $row)
{
?>
<li><?php echo $row['name'] ?> </li>
<?php
$i++;
}
?>
For listing the following code i have used
<?php
if(isset($id)) {
$queryImg = "SELECT * FROM data WHERE country='$_REQUEST[id]'";
$resultImg = mysqli_query($con,$queryImg);
$rowResult = mysqli_num_rows($resultImg);
while($rowsImg = mysqli_fetch_array($resultImg)){ ?>
I do not have enough experience in php, so can you please check this code and tell me how to solve this.
remove single quotes like $id=$_REQUEST['id'];
and here $queryImg = "SELECT * FROM data WHERE country='".$_REQUEST['id']."'";
Just remove the quote from the variable $id, like this:
$id=$_REQUEST['id'];

Using PHP while loop on a dynamic page won't work

I am trying to show it all with while loop on a dynamic page, but it wont show anything at all..
if (isset($_GET['id'])) {
$genre = $_GET['id'];
$sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
$result = $db->query($sql);
$row = $result->fetch_assoc();
}else{
$sql = "SELECT ID, genre FROM movstream_genre";
$result = $db->query($sql);
}
<html>
<body>
<ul>
<?php while ( $row = $result->fetch_assoc() ): ?>
<li><?=$row['genre'];?></li>
<?php endwhile; ?>
</ul>
</body>
</html>
Anyone know why it wont show anything on the dynamic page, but if the page is not dynamic it works fine :)
Thanks.
If dynamic page means that your sending an id in Url an you are not getting result.
I think that the problem is that you are using $row = $result->fetch_assoc(); in if condition and also in while.
Please use fetching in one place. Better be in while loop.
Hope it helps.
I figured it out
if (isset($_GET['id'])) {
$genre = $_GET['id'];
$sql = "SELECT ID, genre FROM movstream_genre";
$result = $db->query($sql);
}
This it how it needs to look when its a dynamic page :)
well i think it is, it works now.
please echo the result ...
<li><?php echo $row['genre'];?></li>
And in href, it is only genre OR genre.php
In the first query..
$sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
$result = $db->query($sql);
$row = $result->fetch_assoc();// this line not required
comment this line
$row = $result->fetch_assoc();
It looks like you haven't enclosed the first part of your PHP code in <?php ?> tags

Listing mysqli table entries in html using php

I'm a beginner and I can't figure out for the life of me how to list products from a mysqli table on a webpage using php. Currently I just manage to get the first row to repeat for the number of rows that exist in the table using the following code:
<?php
$p_sql = "SELECT * FROM products";
$p_query = mysqli_query($db_conx, $p_sql);
$productData = mysqli_fetch_array($p_query, MYSQL_ASSOC);
$num = mysqli_num_rows($p_query);
?>
<!DOCTYPE html>
<?php print "There are currently $num rows in the table<P>";
for ($row=0; $row<$num; $row++){
$name = $productData["product_name"];
echo "$name <br>";
};
?>
I know within my for loop I don't include $row but I don't know how to properly include it. Your help would be much appreciated!
Instead of using your for() loop, use a while() loop with your $productData = mysqli_fetch_array($p_query, MYSQL_ASSOC)
<?php
$p_sql = "SELECT * FROM products";
$p_query = mysqli_query($db_conx, $p_sql);
$num = mysqli_num_rows($p_query);
?>
<!DOCTYPE html>
<?php print "There are currently $num rows in the table<P>";
while($productData = mysqli_fetch_array($p_query, MYSQL_ASSOC)){
echo $productData["product_name"]."<br />";
};
?>

error in storing data to mysql however echo gives correct output

in my tableX some datas are there which looks like this
<h1>ghhhhhh!</h1>
http://twitter.com/USERNAME
<h1></h1>
http://3.bp.blogspot.com/_fqPQy3jcOwE/TJhikN8s5lI/AAAAAAAABL0/3Pbb3EAeo0k/s1600/Srishti+Rai1.html
<h1></h1>
http://4.bp.blogspot.com/_fqPQy3jcOwE/TJhiXGx1RII/AAAAAAAABLc/XNp_y51apks/s1600/anus7.html
<h1></h1>
http://cyz.com/_fqPQy3jcOwE/TJhh1ILX47I/AAAAAAAABKk/gX-OKEXtLFs/s1600/4r-2.html
<h1></h1>
http://cyz.com/_fqPQy3jcOwE/TJhiHGgb-KI/AAAAAAAABK8/zEv_41YzMhY/s1600/19+(1).html
<h1></h1>
http://cyz.com/_fqPQy3jcOwE/TJhihkpZZKI/AAAAAAAABLs/zDnlZkerBd8/s1600/Pooja+Gurung.html
when i echo the same php code it gives correct output but when i am storing these details in mysql only one row is getting stored in mysql row.
my code is this
<?php
include('connect.php');
$idmg=$_POST["id"];
$res =mysql_query("select * from tablea");
$row = mysql_fetch_array($res);
if(sus== '0'){
$output=''.$row['content'].'';
echo $output;//this output gives the above result but when i store in db it stores first row
mysql_query ("INSERT INTO tablea (content) VALUES ('$output')");
} ?>
Your insert is failing because you have unescaped data in $output. Take DCoder's advice above and use PDO or mysqli.
Is there a reason you have sql_fetch_array() and not mysql_fetch_array() ?
You also need to iterate through your results if you want more than one row.
<?php
include('connect.php');
$idmg =$_POST["id"];
$res =mysql_query("select * from tablea");
$row = sql_fetch_array($res)
if($sus== '0'){
$output=mysql_real_escape_string($row['content']);
echo $output;
mysql_query ("INSERT INTO tablea (content) VALUES ('$output')");
}
?>
And as #DCoder said, you should be using prepared statements, the code you have now is vulnerable to SQL injection.
Your code some-what corrected:
<?php
include('connect.php');
$idmg=$_POST["id"];
$res = mysql_query("select * from tablea");
$row = mysql_fetch_array($res);
if($sus== '0'){ // what is sus? If variable.. should be $sus
$output = $row['content']; // .'' is literally nothing..
echo $output;
mysql_query ("INSERT INTO tablea (content) VALUES ('$output')");
}
?>
What I think you are trying to do:
<?php
include('connect.php');
$idmg = $_POST["id"]; // not actually used
$res = mysql_query('SELECT * FROM tablea');
while($row = mysql_fetch_array($res)) {
$output = $row['content'];
echo $output;
// do anything else you want.. in your case ?enter the data back in?
mysql_query("INSERT INTO tablea(content) VALUES('$output')");
}
?>
What you should be using:
<?php
$idmg = $_POST['id']; // <-- not actually used
$res = $mysqli_connection->query('SELECT * FROM tablea');
while($row = $res->fetch_array(MYSQLI_ASSOC)) {
$output = mysqli_connection->real_escape_string($row['content']);
echo $output;
// Do whatever else you like
$mysqli_connection->query("INSERT INTO tablea(content) VALUES('$output')");
}
$res->free();
$mysqli_connection->close();
?>

Categories