3

I'm just trying to show a basic list or items from my database but for some reason its not showing the first item so if there is only one thing in a category it doesn't show anything but if there's 2 it will show one item. I've added the code below.

query

 //this query will fetch 4 records only!
 $latestVideos = mysql_query("SELECT * FROM table ORDER BY date DESC LIMIT 5")or die(mysql_error());

 $posts = mysql_fetch_array($latestVideos);

While loop

 while($posts = mysql_fetch_array($latestVideos)){

 $dateFormat= $posts['video_date'];
 $newDate=date('d-m-Y',strtotime($dateFormat));

 echo $posts['video_title']
 echo $newDate;
 echo $posts['video_embed'];
 }
huddds
  • 1,045
  • 6
  • 27
  • 47

3 Answers3

8

mysql_fetch_array is used to return a row of the array at each iteration of the while loop.

The extra mysql_fetch_array takes the first row into post, and in the first iteration of the while loop it puts the second row into the post.

This is what you should be doing.

$latestVideos = mysql_query("SELECT * FROM table ORDER BY date DESC LIMIT 5")or die(mysql_error());

 while($posts = mysql_fetch_array($latestVideos)){
 //do stuff here
 }
Anurag Ramdasan
  • 4,189
  • 4
  • 31
  • 53
3

It looks like the first time you call "$posts = mysql_fetch_array($latestVideos);" the first row is retrieved and removed from $latestVideos. and the while loop then loops through the other rows.

Sy Holloway
  • 419
  • 4
  • 5
2

You are fetching once with the mysql_fetch_array, but then not using the results. You're reading the first row and then throwing it away.

$posts = mysql_fetch_array(...); # Reads 1st row
while ( $post = mysql_fetch_array() ) { # reads 2nd-Nth row
}
Andy Lester
  • 91,102
  • 13
  • 100
  • 152