While-loop misses out on the last row in SQL database table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leifsod
Forum Newbie
Posts: 5
Joined: Fri May 01, 2009 8:22 pm

While-loop misses out on the last row in SQL database table?

Post by leifsod »

Hi all!

I have made a really simple "newsfeed" on a webpage, which seems to be working fine, except that it doesn't display the last added row. I have to add another, blank row of data in the database in order for it to show the previous (last) one.. any ideas on why this happens? :?

Things I've tried:
* Taking away the LIMIT
* Changing LIMIT to only 4
* Changing ORDER to ASC

...but that's all I can think of with my VERY limited knowledge of PHP/MySQL.. Any help is much appreciated!

Best Regards // Leif

Part of my code:

Code: Select all

 
<?php
"SELECT * 
FROM `tip`
ORDER BY `tip`.`id` DESC
LIMIT 0,4
");
 
$row = mysql_fetch_array($result);
 
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . "<span class='header'>" . $row['header'] . "</span>" . "<p>" . $row['text'] . "</p>" . "</td>";
  echo "</tr>";
  
echo "</table>";
echo "<br>";
echo "<p>Added by " . "<em>". $row['name'] . "</p></em><br>";
}
 
mysql_close($con);
?>
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: While-loop misses out on the last row in SQL database table?

Post by ldougherty »

Hello,

In your SQL query you are limiting the results to only 4 lines. Without seeing your table structure/data there is no way to know for sure if this is your issue but if you are trying to return all rows from a table you should remove the LIMIT 0,4 from your query.

# "SELECT *
# FROM `tip`
# ORDER BY `tip`.`id` DESC
# LIMIT 0,4
# ");

Try simplifying the query all together by using this..

$result = mysql_query("SELECT * FROM `tip`");
while($row = mysql_fetch_array($result)) {

//Output here.

}

I also noticed that in your output you have an end table tag but there is no beginning table tag. I'm assuming the <table> tag is before the while statement and the </table> tag is repeated several times in the while statement. You may wish to look at your structure because mySQL may be returning all of the correct results and you might not be showing them. Try adding a border to your table to look for empty spaces etc.
leifsod
Forum Newbie
Posts: 5
Joined: Fri May 01, 2009 8:22 pm

Re: While-loop misses out on the last row in SQL database table?

Post by leifsod »

Thank you so much for your reply; it worked like magic :). The LIMIT was there on purpose, but changing the code to your "simplification" made everything show up exactly like I wanted it to. Thank you again for the help! :bow:

Best Regards // Leif

P:S: Oh, and the table-tag was a leftover from something I had before, which I now removed..thanks!
Post Reply