0

I don't know how to join tables and fetch informations from each of them. I have clients, who has made reservations and when I click on specific client I need not only information about him from clients table, but also need information related to him by id from reservations table.

This is my ER diagram for better database understanding:

enter image description here

In the overview table of all reservations, based on its status I am using this query:

<?php
      $query = $conn->query("SELECT * FROM `reservations` NATURAL JOIN `clients` NATURAL JOIN `houses` WHERE `status` = 'Pending' ORDER BY firstName") 
      or die(mysqli_error());
      hile($fetch = $query->fetch_array()){
?>

It works perfectly fine, but now I need to display the reservations related only to the specific user I clicked on via link based on ID, example:

<td> <a href = "./Client-Detail.php?client_id=<?php echo $fetch['client_id']?>">
         <?php echo $fetch['firstName']." ".$fetch['lastName']?></a></td>  

I am not quite sure the right way how to display the reservation data of specific client.

  • You can try something like that: `SELECT * FROM `reservations` NATURAL JOIN `clients` WHERE `reservation.client_id` = 1 and `status` = 'Pending' ORDER BY reservation_id` , it will get all reservations of client with id = 1 and which has status = Pending. – Serghei Leonenco Jan 04 '21 at 20:43
  • But it needs to be dynamic, so I can not specify the exact ID in query, it would has to be something like `....WHERE reservations.client_id = client_id`, but it does not work. –  Jan 04 '21 at 20:45
  • You should use `prepared statements` in this case, i showed the `sql` which suppose to return data what you need. Here is the link: https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php#example-1646 – Serghei Leonenco Jan 04 '21 at 20:47
  • Shouldn't the ER diagram show a link between `Reservations` and `Clients`? – Barmar Jan 04 '21 at 20:55
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jan 04 '21 at 22:16

2 Answers2

0

You need to put the client_id check in the WHERE clause instead of checking status.

$stmt = $conn->prepare("
    SELECT * 
    FROM `reservations` 
    NATURAL JOIN `clients` 
    NATURAL JOIN `houses` 
    WHERE client_id = ?");
$stmt->bind_param("i", $_GET['client_id']);
$query = $stmt->execute();
while ($fetch = $query->fetch_array()) {
    ...
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You made a good point, wasn't that hard actually :) Thank you for solving it! –  Jan 04 '21 at 21:05
  • I have a question though, right now I can see only one reservation, even though the client I clicked on made more than one. DO you know how to handle that ? –  Jan 04 '21 at 21:31
  • That shouldn't happen. Are all the reservations `status = Pending`? – Barmar Jan 04 '21 at 21:32
  • No, they are under different status, but I deleted that from the query: `$stmt = $pdo->prepare('SELECT * FROM `reservations` NATURAL JOIN `clients` NATURAL JOIN `houses` WHERE client_id = ? ');` –  Jan 04 '21 at 21:36
  • Then it should find them all. – Barmar Jan 04 '21 at 21:37
  • Alright, I think it will be a mistake by using different ways of database manipulation and it fetching only the specific reservation from table. –  Jan 04 '21 at 21:39
-1
SELECT r.*, c.*, h.* 
FROM `reservations` r 
LEFT JOIN `clients` c ON (c.client_id = r.client_id) 
LEFT JOIN `houses` h ON (h.house_id = r.house_id)
WHERE r.status = 'Pending' AND c.client_id = '$0'
ORDER BY c.firstName ASC

This should work and bind ID via PDO or MySQLi

  • There's no `id` column in the `client` or `houses` tables. – Barmar Jan 04 '21 at 20:53
  • How does this solve the problem of showing the data for a specific client ID? – Barmar Jan 04 '21 at 20:54
  • I believe he can add simple where clause :-) i fixed IDs. – Michal Hudák Jan 04 '21 at 20:57
  • If he knew how to add that simple where clause he wouldn't be asking the question, would he? – Barmar Jan 04 '21 at 20:58
  • All you've done is replace `NATURAL JOIN` with explicit `ON` clauses that do the same thing. – Barmar Jan 04 '21 at 20:59
  • I fixed for you, even with client ID part – Michal Hudák Jan 04 '21 at 20:59
  • Also you haven't notice but its specified table at every column client_id especialy.. since in his there is two client_id columns... some MySQL configs will produce error othervise – Michal Hudák Jan 04 '21 at 21:07
  • That's what `NATURAL JOIN` does: it means to join on all the same-named columns. – Barmar Jan 04 '21 at 21:07
  • What is `$0`? I've never seen that used except in `preg_replace()`. – Barmar Jan 04 '21 at 21:11
  • Did not know that... thank's for that... My SQL also assuming that client or house don't have to exists, but i don't know if that's design you're going for – Michal Hudák Jan 04 '21 at 21:16
  • A foreign key requires that the related records exist. You can't make a reservation for a nonexistent house. – Barmar Jan 04 '21 at 21:16
  • You can make a reservation and than delete house for example... Not in all cases you delete reservation when house or client gets deleted (you can have valuable information in that reservation that you dont want to throw out) – Michal Hudák Jan 04 '21 at 21:21
  • If you try to delete a record that's referenced by a foreign key you get an error, unless the FK has `ON DELETE CASCADE`, in which case the referencing records are deleted automatically. – Barmar Jan 04 '21 at 21:22
  • How about ON DELETE SET NULL? You can have reservation where there is for example house_id NULL in that case, you're SQL wont show this record with NATURAL JOIN if i'm not mistaken... my will show this record with NULL on columns from house table... and again this could be desired state – Michal Hudák Jan 04 '21 at 21:27
  • The same thing happens if you use `ON c.client_id = r.client_id` instead of `NATURAL JOIN`. `NATURAL JOIN` is just a shortcut, it doesn't affect how the tables are related. – Barmar Jan 04 '21 at 21:33
  • What is `$0`? Shouldn't this be the placeholder instead? – Dharman Jan 04 '21 at 22:16