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.
Try to do a var_dump on $row, you may find out more on what went wrong, if no var_dump appears, no result return was returned, use mysqli_error to find out what went wrong
mysqli_error($con);
while($row = mysqli_fetch_array($result))
{
var_dump($row);
}
It could be a case issue
$row['u_name']
may be is
$row['U_name']
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
Maybe it's just that I suffer insomnia, but that should work
You want to retrieve all the contacts of user with id 2 so basically your query would look like this:
SELECT u.u_name, u.u_email
FROM contact_list cl
JOIN user u
ON cl.u_contact_id = u.u_id
WHERE cl.u_id = 2
What the above query does is, it will join the users information based on the u_contact_id
and will list all the names and emails registered to it.
And your PHP would look like this:
<?php
// your database info here
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
$con = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
if($con->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
if (!$stmt = $con->prepare("SELECT u.u_name, u.u_email
FROM contact_list cl
JOIN user u
ON cl.u_contact_id = u.u_id
WHERE cl.u_id = ?"))
die('Prepare Error: ' . $con->error);
$id = 2;
if (!$stmt->bind_param('i', $id))
die('Bind Parameters Error ' . $stmt->error);
if (!$stmt->execute())
die('Select Query Error ' . $stmt->error);
?>
<table border="1">
<th>Contact List</th>
<tr>
<th>Name</th>
<th>E-mail</th>
</tr>
<?php
$stmt->bind_result($name,$email);
while ($stmt->fetch())
{
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $email; ?></td>
</tr>
<?php
}
echo "</table>";
$stmt->close();
$con->close();
In your query:
=
sign. user
as both u
and cu
, but seems like you can get by with just one. Try the following:
SELECT cu.u_name, cu.u_email
FROM contact_list c, user u // Or alternatively: FROM contact_list AS c, user AS u
WHERE c.u_id = 2
AND c.u_contactId = c.u_id
AND c.u_id = u.u_id
By the way, when printing the <table>
, you need to place <th>
inside <tr>
for valid HTML.
Also, in your $result = mysql_query (....)
you ended it with or die (mysql_error())
. It should be or die (mysqli_error())
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.