hi guys,
I have created a database using mysql and interfacing it with an html web page using php. However when I try to display for example all the data in a table in the database, it only starts displaying from the econd entry but does not display the first.

<?php
mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select("database") or die(mysql_error());
$table_data=mysql_query("SELECT * FROM tablename) or die(mysql_error());
$truedata=mysql_fetch_array($table_data);
echo "<table>"
echo "<tr><th>col1</th><th>col2</th></tr>";
while($truedata=mysql_fetch_array($table_data)){
echo "<tr><td>".$truedata['col1']."</td><td>".$truedata['col2']."</td></tr>";
}
echo "</table>";
?>

The above only displays entries 2 to the end leaving out entry 1.
Pliz help.
Thanx

It appears that you've included $truedata = mysql_fetch_array() twice but not used it the first time (thus the probable cause of the loss of your first row).

Try it now and let me know if that was it ...

<?php
mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select("database") or die(mysql_error());
$table_data=mysql_query("SELECT * FROM tablename) or die(mysql_error());
//$truedata=mysql_fetch_array($table_data); // REMOVED
echo "<table>"
echo "<tr><th>col1</th><th>col2</th></tr>";
while($truedata=mysql_fetch_array($table_data)){
echo "<tr><td>".$truedata['col1']."</td><td>".$truedata['col2']."</td></tr>";
}
echo "</table>";
?>

Ciao

Instead of while use a do while loop it will automatically fix the problem.
And also remember it is a good practice to use do while when displaying data from database.

Instead of while use a do while loop it will automatically fix the problem.
And also remember it is a good practice to use do while when displaying data from database.

I'm not arguing with you architact, but I'm wondering why use a do { ... } while () for database rows?

If I understand that will automatically perform the action at least once. So you should check your mysql_num_rows before executing it at all, and also need to get the mysql_fetch_row before the loop and in the loop?

Curious?

I am recommending it because he is using fetch array before while loop, and then the program comes to loop and encounters the fetch array again and moves to next index in array. that's why if he is using fetch array before loop he should use do while.

Thanks guys. I removed the first "mysql_fetch_array()" and it worked. Not yet tried out the do while loop though but it sounds a great idea. Will do it.
Thanx alot for that.

If you removed the first mysql_fetch_row then stick to a while loop, if you left in that first fetch_row then do a do{}while() loop.

Ciao

This question has already been answered. Start a new discussion instead.