$sql1=mysql_query("SELECT * FROM Persons", $con);
echo "<table border="3">
<tr>
<th>Name</th>
开发者_如何学运维 <th>Age</th>
</tr>";
while($info=mysql_fetch_array($sql1))
{
echo "<tr>";
echo "<td>" . $info['fname'] . "</td>";
echo "<td>" . $info['age'] . "</td>";
echo "</tr>";
}
echo "</table>";
this code is a part of a code which is trying to retrieve data from the table"Persons", there is some error in this part of the code..
You have double quotes in the quoted html. Try using single quotes in stead, i.e.
echo "<table border='3'> <--- here
<tr>
<th>Name</th>
<th>Age</th>
</tr>";
Your code looks ok, except for the unescaped double quotes:
It should be:
echo "<table border=\"3\"> ... ";
or
echo '<table border="3"> ... ';
Make sure it is enclosed in <?php
and ?>
.
Also make sure your db columns names of fname
and age
really exist....
Make sure you're getting what you think back from the DB, by using print_r($info)
or var_dump($info)
.
Finally, your connection $con
could be broken / not working. You can check that by using:
if ( ! $con ) {
die('Could not connect: ' . mysql_error());
}
$sql1 = mysql_query("SELECT * FROM Persons", $con);
...
Beside double quotes in second line.
mysql_fetch_array
return array indexed by integer if you want asoc array indexed by fields use mysql_fetch_assoc
while ($info=mysql_fetch_assoc($sql1)) {
...
}
精彩评论