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'];
 }

3 Answers 3

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
 }
Sign up to request clarification or add additional context in comments.

2 Comments

so could i just do $posts = $latestVideos ?
Thanks a lot, that's been bugging me for a while
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.

Comments

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.