0

我有两张桌子,一张usersuser_id, user_name, school_idand调用,另一张用, , andsubcat_id调用。我制作了一个带有 select 语句的搜索表单,该语句从第一个表中收集结果,基于哪个和/或用户在搜索表单上选择的结果。现在,我想用,和显示结果。但就像在我的选择语句中一样,我只从第一个表中选择,而不是从第二个表中选择。如果他们不在 select 语句中,我如何显示我的用户结果?schoolsschool_idschool_nameschool_decilesubcat_idschool_iduser_nameschool_nameschool_decileschool_decileschool_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);
4

2 回答 2

0

正如@Rasclatt 建议的那样,您需要使用JOIN

if ($subcat_number !== '0') {
    if ($school_number === '0') { //if a school isn't selected
        $sql = "SELECT name, school_name, school_decile  FROM users INNER JOIN schools on users.school_id = schools.school_id  WHERE subcat_id=$subcat_number";
    } else { //if a school is selected
        $sql = "SELECT name, school_name, school_decile FROM users INNER JOIN schools on users.school_id = schools.school_id 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>";

    }
} else {
    echo "<td>No Expert Found</td></tr>";
}
echo "</table>";
于 2014-10-25T02:51:36.567 回答
0

你应该做这样的事情:

<?php
    if($subcat_number !== '0') {
            $sql = "SELECT users.name, users.school_name, users.school_decile FROM users INNER JOIN schools on users.school_id = schools.school_id  WHERE users.subcat_id = $subcat_number";

            if($school_number === '0')
                $sql .= " AND schools.school_id = '$school_number'";

            $result =   mysqli_query($con, $sql);
            $found  =   mysqli_num_rows($result); ?>
    <table>
        <tr>
            <th id='namecol'>Search Results:</th>
            <th id='schoolcol'></th>
        </tr><?php

            if($found > 0) {
                    while ($row = mysqli_fetch_array($result)) { ?>
        <tr>
            <td id='namecol'>Name: <?php echo $row['name']; ?></td>
            <td>School: <?php echo $row['school_name']; ?><br>Decile of School: <?php echo $row['school_decile']; ?></td>
        </tr>
                <?php   }
                }
            else { ?>
        <tr>
            <td>No Expert Found</td>
        </tr><?php 
                } ?>
    </table><?php
        } ?>
于 2014-10-25T03:44:26.350 回答