3

我只是想从我的数据库中显示一个基本列表或项目,但由于某种原因,它没有显示第一个项目,所以如果一个类别中只有一个项目,它不会显示任何内容,但如果有 2 个,它将显示一个项目. 我在下面添加了代码。

询问

 //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 循环

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

3 回答 3

8

mysql_fetch_array用于在 while 循环的每次迭代中返回数组的一行。

extramysql_fetch_array将第一行放入 post 中,并在 while 循环的第一次迭代中将第二行放入 post 中。

这是你应该做的。

$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
 }
于 2013-03-09T20:15:49.733 回答
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.

于 2013-03-09T20:18:29.027 回答
2

您使用 mysql_fetch_array 获取一次,但随后不使用结果。您正在阅读第一行,然后将其丢弃。

$posts = mysql_fetch_array(...); # Reads 1st row
while ( $post = mysql_fetch_array() ) { # reads 2nd-Nth row
}
于 2013-03-09T20:18:11.257 回答