I am new to PHP & MySQL and was just playing around to see if I could get this to work. Apparently I can't. I have 2 tables.
Table 1 - Users
userid -- primary key
fname
lname
companyid -- foreign key
Table 2 - Companies
companyid -- primary key
name
I am displaying the data in a table and only get the number (companyid). How can I get the name of the company to show up in the table?
Here is my code:
$result = mysql_query("SELECT * FROM users WHERE id = '$q'") or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Company</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['companyid'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close();
?>
I tried inserting this code:
$result = mysql_query("SELECT name FROM companies JOIN users ON users.companyid=companies.companyid WHERE id = '$q'") or die(mysql_error());
but still got nothing. What am I doing wrong? How can I get all the data to show?
Your help is much appreciated.