我只是想从我的数据库中显示一个基本的列表或项目,但是由于某种原因,它没有显示第一个项目,所以如果一个类别中只有一个项目,它不会显示任何东西,但是如果有两个项目,它将显示一个项目。我已经添加了下面的代码。
查询
//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'];
}发布于 2013-03-10 04:15:50
mysql_fetch_array用于在while循环的每次迭代中返回数组的一行。
额外的mysql_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-10 04:18:29
看起来就像您第一次调用"$posts = mysql_fetch_array( $latestVideos );“第一行是从$latestVideos中检索和删除的。然后while循环循环遍历其他行。
发布于 2013-03-10 04:18:11
您使用mysql_fetch_array获取一次,但随后不使用结果。您正在读取第一行,然后将其丢弃。
$posts = mysql_fetch_array(...); # Reads 1st row
while ( $post = mysql_fetch_array() ) { # reads 2nd-Nth row
}https://stackoverflow.com/questions/15315407
复制相似问题