我现在正在学习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调用脚本。
提前致谢
答案 0 :(得分:3)
如果mysqli_num_rows($result)
大于1,则只返回行。这意味着它仅在两个或更多行时才有效。
答案 1 :(得分:1)
if (mysqli_num_rows($result) > 1) {
您只排除只有一行的结果。改为:
if (mysqli_num_rows($result) > 0) {