I have two table, a class table and a professor table. Using mySQL,
describe class
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| class | varchar(225) | NO | | NULL | |
| description | varchar(225) | NO | | NULL | |
| prof_id | int(11) | NO | MUL | NULL | |
+-------------+--------------+------+-----+---------+----------------+
The prof_id is the foreign key, and the other table
describe professor
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| office | varchar(20) | NO | | NULL | |
| phone | varchar(50) | NO | | NULL | |
| email | varchar(50) | NO | | NULL | |
| username | varchar(100) | NO | | NULL | |
| password | varchar(100) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
id is the primary key that class.prof_id is referencing to. I gotta web app, and I want to print out professor.name based on the given prof_id. (ie: if a professor named John Doe id = 1, and if 1 is in prof_id, I want John Doe to be printed. Here is my php code:
<?php
require_once('connect.php');
session_start();
$resultQuery = mysqli_query($mysqli, "SELECT * FROM professor");
?>
------SKIPPING OVER USELESS HTML/CSS-------
<table>
<tr>
<u><th>Name</th> <th>Class</th> <th>Description</th> <th>Professor</th> <th>Update</th></u>
</tr>
<?php
while($user_data = mysqli_fetch_array($resultQuery))
{
echo "<tr>";
echo "<td>".$user_data['class']."</td>";
echo "<td>".$user_data['description']."</td>";
echo "<td>".$user_data['prof_id']."</td>";
echo "<td><button><a href='editClasses.php?id=$user_data[id]'>Edit</a></button><button><a href='deleteClasses.php?id=$user_data[id]'>Delete</a></button></td></tr>";
}
?>
</table>