tags:

views:

73

answers:

3
$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..

+4  A: 

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>";
Ivo van der Wijk
it worked...but only showing the table with table headers NAME and AGE...what abt the data from the database...is the rest of the code fully correct??
Vinod K
You can always try to print_r ( $info ); to see what is currently being returned through the while loop.
judda
@Vinod K: Do you have data in your table?
Felix Kling
there is data..i can see the table but its empty..but with each new record i can see the size of the empty table growing..
Vinod K
@judda print_r ( $info ); helped me solve the problem...thnx :-)
Vinod K
+1  A: 

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);
...
Peter Ajtai
when i do print_r($info)..i get the following output..recordAddedArray ( [0] => Bourne [Firstname] => Bourne [1] => 35 [Age] => 35 ) Array ( [0] => Jason [Firstname] => Jason [1] => 35 [Age] => 35 )
Vinod K
Hey it worked when i changed it from "fname" to "Firstname"....But the name of the textboxes were fname and age ...
Vinod K
@Vinod K - That's the problem then. `name` should be accessed `$info["Firstname"]`. - How the info is stored in the DB is independent of how it's shown in the form.
Peter Ajtai
ookkk...i got it..thanx guys..
Vinod K
@Peter Ajtai Thnx...:-)
Vinod K
A: 

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)) {
   ...
}
jcubic