Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP while循环不显示第一项?_Php_Mysql_Loops_While Loop - Fatal编程技术网

PHP while循环不显示第一项?

PHP while循环不显示第一项?,php,mysql,loops,while-loop,Php,Mysql,Loops,While Loop,我只是想从我的数据库中显示一个基本列表或项目,但由于某些原因,它没有显示第一个项目,因此如果类别中只有一个项目,它不会显示任何内容,但如果有两个,它将显示一个项目。我在下面添加了代码 质疑 While循环 while($posts = mysql_fetch_array($latestVideos)){ $dateFormat= $posts['video_date']; $newDate=date('d-m-Y',strtotime($dateFormat)); echo $pos

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

质疑

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

mysql\u fetch\u array
用于在while循环的每次迭代中返回一行数组

额外的
mysql\u fetch\u数组将第一行放入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
 }

您使用mysql_fetch_数组获取一次,但不使用结果。你在读第一行,然后把它扔掉

$posts = mysql_fetch_array(...); # Reads 1st row
while ( $post = mysql_fetch_array() ) { # reads 2nd-Nth row
}

第一次调用“$posts=mysql_fetch_array($latestVideos);”时,会从$latestVideos中检索并删除第一行。然后while循环遍历其他行。

那么我可以只做$posts=$latestVideos吗?非常感谢,这已经困扰了我一段时间了
$posts = mysql_fetch_array(...); # Reads 1st row
while ( $post = mysql_fetch_array() ) { # reads 2nd-Nth row
}