I have a database with 2 tables User, Contact_List.
The User table for example looks like this:
U_id | U_email | U_password | U_mobileNo | U_name |
---------------------------------------------------
1 | a@b.c | aaa | 1234567 | Adam |
2 | b@b.c | bbb | 1234567 | Ben |
3 | c@b.c | ccc | 1234567 | Carl |
The Contact_list table looks like this. This table is table just consisting of foreign keys that relate two users together
U_id | U_contact_id
-------------------
1 | 2
2 | 3
Now the problem is my SQL/PHP query to display a table that consists of a specific users list of contacts.
This SQL query works fine and gives the results I want:
"SELECT cu.u_name, cu.u_email
FROM contact_list = c, user = u, user = cu
WHERE c.u_id = 2
AND c.u_contactId = c.u_id
AND c.u_id = u.u_id"
But this PHP code:
$con = mysqli_connect("dbname","dbuser","pbpass","db");
//Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT cu.u_name, cu.u_email
FROM contact_list = c, user = u, user = cu
WHERE c.u_id = 2
AND c.u_contactId = c.u_id
AND c.u_id = u.u_id" ) or die(mysql_error());
echo "<table border='1'>
<th>Contact List</th>
<tr>
<th>Name</th>
<th>E-mail</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['u_name'] . "</td>";
echo "<td>" . $row['u_email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
mysqli_close($con);
?>
That code just prints a blank table on the page.
What am I doing wrong here?
I am a complete noob using PHP any help or suggestions would be appreciated.