我有两张桌子,一张users
用user_id
, user_name
, school_id
and调用,另一张用, , andsubcat_id
调用。我制作了一个带有 select 语句的搜索表单,该语句从第一个表中收集结果,基于哪个和/或用户在搜索表单上选择的结果。现在,我想用,和显示结果。但就像在我的选择语句中一样,我只从第一个表中选择,而不是从第二个表中选择。如果他们不在 select 语句中,我如何显示我的用户结果?schools
school_id
school_name
school_decile
subcat_id
school_id
user_name
school_name
school_decile
school_decile
school_name
if ($subcat_number !== '0') {
if ($school_number === '0') { //if a school isn't selected
$sql = "SELECT * FROM users WHERE subcat_id=$subcat_number";
} else { //if a school is selected
$sql = "SELECT * FROM users WHERE subcat_id=$subcat_number AND school_id=$school_number ";
}
}
$result = mysqli_query($con, $sql);
$found = mysqli_num_rows($result);
//results table
echo "<table>
<tr>
<th id='namecol'>Search Results:</th>
<th id='schoolcol'></th>
</tr><tr>";
if ($found > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<td id='namecol'>Name: " . $row['name'] . " </td><td>School: " . $row['school_name'] . " <br>Decile of School: " . $row['school_decile'] . "</td>";
//obviously, the $row['school_name'] and $row['school_decile'] doesn't work as they weren't selected in the select statements
}
} else {
echo "<td>No Expert Found</td></tr>";
}
echo "</table>";
新编辑 按照以下建议进行 JOIN 后,出现此警告并且显示的结果是错误的。
警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,布尔值在第 74 行的 /var/www/projectv5/search.php 中给出
第 74 行是
$found = mysqli_num_rows($result);