oops!! used the code tag wrongly!! sorry.

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

Member Avatar for langsor

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

yes...
remove the line: $truedata = mysql_fetch_array()

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.

Member Avatar for langsor

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.

Member Avatar for langsor

Ahhh, makes perfect sense. Thanks for the explanation.

Ciao

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.

Member Avatar for langsor

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.