$ i不会在while循环中递增

时间:2014-11-16 09:52:35

标签: php mysql while-loop

以下是我的PHP代码,它只提供i =0但是在循环中我正在递增$i但是它总是返回i 0而while循环只工作一次,虽然我的查询SELECT * FROM events WHERE DATE(event_date) < CURDATE()phpmyadmin中出现时返回了7条记录。让我知道我在这里做错了什么?

代码 -

<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/app/'."config.php";
error_reporting(E_ALL);

if( $_POST['number'] == 'all' ) {
    $eventArr = array();
    $myarray = array();
    $query = "SELECT * FROM events WHERE DATE(`event_date`) < CURDATE()";
    $result = mysql_query($query);
    $i =0;
    while($row = mysql_fetch_assoc($result)) {

        $eventArr[$i] = array('event_data'=> $row);

        // Get image For an event
        $event_id = $row['id'];

        $query = "SELECT * FROM event_images WHERE event_id = $event_id ORDER BY `uploaded_date` DESC LIMIT 0,1";
        $result = mysql_query($query);
        $eventImgArr = array();
        while($row = mysql_fetch_assoc($result)) {
            $eventImgArr[] = $row;
        }
        $eventArr[$i]['event_image'] = $eventImgArr;

        // Get venue details for the event
        $venue_id = $row['venue_id'];
        $eventVenArr = array();
        $query = "SELECT * FROM `venues` WHERE id = $venue_id";
        while($row = mysql_fetch_assoc($result)) {
            $eventVenArr[] = $row;
        }
        $eventArr[$i]['venue_detail'] = $eventVenArr;
        echo $i, " -- ";
        $i++;
    }
$myarray = array('response'=>'1','message'=>'Event data', 'data'=>$eventArr);
echo json_encode($myarray);
return;
}

1 个答案:

答案 0 :(得分:5)

您正在为其他查询重用$result变量,这会破坏主循环所需的值。

P.S。此外,您实际上并没有对场地详细信息执行查询。