搜索表单选择结果

时间:2014-10-25 01:46:41

标签: php mysql sql search-form

我有两个表,一个名为usersuser_iduser_nameschool_idsubcat_id,另一个名为schoolsschool_idschool_nameschool_decile。我使用select语句创建了一个搜索表单,该表单根据用户在搜索表单中选择的subcat_id和/或school_id来收集第一个表格的结果。现在,我想使用user_nameschool_nameschool_decile显示结果。但是在我的select语句中,我只从第一个表中选择而不是从第二个表中选择。如果用户不在select语句中,如何使用school_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);

2 个答案:

答案 0 :(得分: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>";

答案 1 :(得分: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
        } ?>