Select Friends id from different table
Posted: 11 Jul 2017, 09:27
I have a friend-system that works with a database
I need from another table the user-id (id), so i can link to their profile
table: my_friends
table: user
This is without the id from eatch user:
Now i tried this but it is complete wrong
I need from another table the user-id (id), so i can link to their profile
table: my_friends
Code: Select all
id username friend
68 andreamein Wendy P
67 Wendy P andreamein
66 Vrouw Holland Wendy P
65 Wendy P Vrouw Holland
Code: Select all
id name email pass
0 Anonymous anonymous@hotmail.com
1 Community -community@hotmail.com
Code: Select all
<?php
// connect to the "tests" database
$conn = new mysqli('127.0.0.1', 'comments', '123456', 'comments');
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$sql = "SELECT `id`, `username`, `friend` FROM `my_friends` WHERE username='$users[name]' ";
// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
while($row = $result->fetch_assoc()) {
echo '<br> '. $row['friend']. ' ' ;
}
}
else {
echo '<br>No Friends';
}
$conn->close();
?>
Code: Select all
// SELECT sql query
$sql = "SELECT A.`username` , B.`name` , B.`id`
FROM `my_friends`
AS A
INNER JOIN `user` AS B
ON B.`name` = A.`username`
WHERE username='$users[name]' ";