0

我现在正在学习 MySql 一段时间,我认为制作一个可以搜索飞机的有趣项目会很有趣。问题是我想在 PHP 中选择具有给定数量引擎的飞机。这适用于偶数,但是当使用奇数时会出现问题,但仅在 PHP 代码中,而不是当我在 PHPMyadmin 中将其用作查询时。

代码:

$result=mysqli_query($con, "SELECT * FROM civilPlanes WHERE engines=3");
//Normally 3 would be a POST variable

if (!$result) {
    die('Error: '.mysqli_error($con));
}

if (mysqli_num_rows($result) > 1) {
    echo '
    <tr>
        <th>Manufacturer</th>
        <th>Type</th>
        <th>Seats (max)</th>
        <th>Tumbnail</th>
        <th>Engines</th>
    </tr>';

    while ($row=mysqli_fetch_array($result)) {
        echo '
        <tr> 
            <td>' . $row['manufacturer'] . '</td>
            <td>' . $row['type'] . '</td> 
            <td>' . $row['maxSeats'] . '</td>
            <td>' . $row['thumbnail'] . '</td>
            <td>' . $row['engines'] . '</td>
        </tr>';
    }
}

else {
    echo 'Nothing found';
}

我正在搜索的飞机的行是:

id manufacturer      type  seats engineType  engines
21 McDonnell Douglas MD-11 410   turbofan    3

每架发动机数量不同的飞机都可以正常工作。

哦,脚本是用 AJAX 调用的。

提前致谢

4

2 回答 2

3

mysqli_num_rows($result)如果大于一,您只会返回行。这意味着它仅在有两行或更多行时才有效。

于 2013-11-04T21:27:15.997 回答
1
if (mysqli_num_rows($result) > 1) {

您排除了只有一行的结果。将其更改为:

if (mysqli_num_rows($result) > 0) {
于 2013-11-04T21:27:11.423 回答