Reputation: 7
I have two tables:
Table 1
Table 2
How can I select and order this 2 tables in one query. I've tried union
and left join
, but did not work.
$result = mysqli_query($database->connection,
"SELECT *
FROM contacts
WHERE owner = '$session->username'
ORDER BY name ASC ,bedrijfsnaam ASC")
or die(mysqli_error());
while($roww = mysqli_fetch_array($result)){
echo $roww['email'];
echo $roww['name'];
}
Table contacts_group:
$result = mysqli_query($database->connection,
"SELECT *
FROM contacts_group
WHERE owner = '$session->username'
ORDER BY group_name ASC")
or die(mysqli_error());
while($roww = mysqli_fetch_array($result)){
echo $roww['mail'];
echo $roww['group_name'];
}
Upvotes: 0
Views: 97
Reputation: 2535
Escape your variables. You should not put the php variable unescaped in sql query. You can suffer from sql injections.
If you want to join two tables by foregin key you can do:
SELECT * FROM contacts, JOIN contacts_group ON contacts_group.id = contacts.group_id
WHERE contacts.owner = '$session->username' ORDER BY contacts.name
But you are missing group_id in contacts table or some foregin key to connect two tables.
Upvotes: 1
Reputation: 4560
Before do it read this topic, after try to use mysql join construction
Upvotes: 0