PHP while循环while循环只返回有限的行

时间:2014-02-26 22:08:46

标签: php mysql

我正在回到PHP和MySQL编码,我有一个搜索页面,使用2个while循环返回表格中的某些行。

第一个while循环搜索数据库,第二个循环搜索第二个DB以查找注册匹配,以确定设备类型。该脚本正在运行,但是,当第一个while循环返回2个以上的结果时,第2个while循环只显示设备上的前2个返回,欢迎任何帮助。

$connect = mysql_connect("$dbhost", "$dbuname", "$dbpass") or die("Couldn't connect.");
$db = mysql_select_db($db_name, $connect) or die("Couldn't select database.");
$query = "SELECT * FROM `db1` WHERE `dest_id` =$ap ORDER BY video_date DESC, operator_name, operator_tail ASC";
$result = mysql_query($query,$connect) or mysql_error();

while ($r = mysql_fetch_array($result)) {
    $row_color = ($row_count % 2) ? $color1 : $color2;
    $time_update = $r[video_date];
    $str_time = date('M. d, Y', $time_update);

    echo ("<tr><td bgcolor='$row_color'>&nbsp;<a href='$server?al=$r[operator_id]'>$r[operator_name]</a>
           </td><td bgcolor='$row_color' align='center'>&nbsp;<a href='$r[operator_ICAO]$r[flight_num]' target='_blank'>$r[flight_num]</a>
           </td><td bgcolor='$row_color'>&nbsp;<a href='$r[operator_tail]' target='_blank'>$r[operator_tail]</a>
           </td><td bgcolor='$row_color' align='center'>&nbsp;<a href='$r[video_url]' target='_blank'>$r[video_url]</a>
           </td><td bgcolor='$row_color' align='center'>");

    $connect2 = mysql_connect("$dbhost", "$dbuname", "$dbpass") or die("Couldn't connect.");
    $query2 = "SELECT * FROM `plane` WHERE `plane_id` =$r[plane_id]";
    $result2 = mysql_query($query2,$connect2) or mysql_error();

    while ($q = mysql_fetch_array($result2)) {
        echo ("&nbsp;$q[plane_type]");
    }

    mysql_close($connect2);
    echo (" </td><td bgcolor='$row_color' align='center'>&nbsp;$str_time
            </td><td bgcolor='$row_color' align='center'>&nbsp;<a href='$server?ap=$r[dest_id]'>$r[dest_name]
            </td><td bgcolor='$row_color' align='center'>&nbsp;$r[vtype_name]
            </td> </tr>");
    $row_count++;
}
mysql_close($connect);

1 个答案:

答案 0 :(得分:0)

好的,我在这里看到一些问题。对于一个mysql_fetch_array将返回一个非关联数组,除非您的系统设置为返回类型MYSQL_BOTH。我建议将脚本重写为mysqli,如下所示:

<?php
$connect = new mysqli("$dbhost", "$dbuname", "$dbpass","$db_name");
$connect->connect_errno ? die("Couldn't connect.") : null ;

$query = "SELECT * FROM `db1` WHERE `dest_id` =$ap ORDER BY video_date DESC, operator_name, operator_tail ASC";
$result = $connect->query($query);

if(!$results->errno):

while ($r = mysql_fetch_array($result)) {
    $row_color = ($row_count % 2) ? $color1 : $color2;
    $time_update = $r[video_date];
    $str_time = date('M. d, Y', $time_update);

    echo ("<tr><td bgcolor='$row_color'>&nbsp;<a href='$server?al=$r[operator_id]'>$r[operator_name]</a>
           </td><td bgcolor='$row_color' align='center'>&nbsp;<a href='$r[operator_ICAO]$r[flight_num]' target='_blank'>$r[flight_num]</a>
           </td><td bgcolor='$row_color'>&nbsp;<a href='$r[operator_tail]' target='_blank'>$r[operator_tail]</a>
           </td><td bgcolor='$row_color' align='center'>&nbsp;<a href='$r[video_url]' target='_blank'>$r[video_url]</a>
           </td><td bgcolor='$row_color' align='center'>");

    $query = "SELECT * FROM `plane` WHERE `plane_id` =$r[plane_id]";

    if($result2 = $connect->query($query)):
        while ($q = $result2->fetch_assoc($result2)) {
            echo ("&nbsp;$q[plane_type]");
        }

        echo (" </td><td bgcolor='$row_color' align='center'>&nbsp;$str_time
                </td><td bgcolor='$row_color' align='center'>&nbsp;<a href='$server?ap=$r[dest_id]'>$r[dest_name]
                </td><td bgcolor='$row_color' align='center'>&nbsp;$r[vtype_name]
                </td> </tr>");
        $row_count++;
    endif;
}
endif;
$connect->close();

此外,如果你可以避免嵌套循环Complexity (N * M)又名O(N 2 )。我会重写SQL语句以使用INNER JOIN一次性获取所有结果。

希望能帮助您找到解决方案并帮助您更快地运行脚本。

<强>更新

这是适用于您的问题的代码。我没有完全更新所有内容以优化它,但这将为您建立一个基线:

Files is here

Working Example here