3

データベースから基本的なリストまたはアイテムを表示しようとしていますが、何らかの理由で最初のアイテムが表示されないため、カテゴリに1つしかない場合は何も表示されませんが、2つある場合は1つのアイテムが表示されます. 以下のコードを追加しました。

クエリ

 //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($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_arraywhileループの各反復で配列の行を返すために使用されます。

エクストラmysql_fetch_arrayは最初の行をポストに入れ、whileループの最初の反復で2番目の行をポストに入れます。

これはあなたがすべきことです。

$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

「$posts= mysql_fetch_array($ latestVideos);」を初めて呼び出すときのようです。最初の行が取得され、$latestVideosから削除されます。そして、whileループは他の行をループします。

于 2013-03-09T20:18:29.027 に答える
2

mysql_fetch_arrayで1回フェッチしていますが、結果は使用していません。あなたは最初の行を読んで、それを捨てています。

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