I have two tables, one called users with user_id, user_name, school_id and subcat_id, and the other called schools with school_id, school_name, and school_decile. I've made a search form with select statement which gathers results from the first table based on which subcat_id and/or school_id the user has chosen on the search form. Now, I want to display the results with the user_name, school_name and school_decile. But as in my select statements, I only selected from the first table and not from the second one. How can I display my results of the users with the school_decile and school_name if they weren't in the select statement?
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>";
New Edit After following the suggestion below of doing a JOIN, this warning came up and the shown result is wrong.
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /var/www/projectv5/search.php on line 74
the line 74 is
$found = mysqli_num_rows($result);