-2

This is my table and data of each table

Table reservation:

+------------------+---------  +---------+
| rid              | r_date    | r_time  |
+------------------+---------  +---------+
| 1                | 2019-12-20| 10:00:00| 
| 2                | 2019-12-20| 10:00:00|
| 3                | 2019-12-20| 10:00:00|
+------------------+-----------+---------+

Table combo:

+------------------+---------------+------------+
| combo_id         | combo_name    | combo_price|
+------------------+---------------+------------+
|    1             | Package 1     | 250.00     | 
|    2             | Package 2     | 250.00     |
|    3             | Package 3     | 250.00     |
+------------------+---------------+------------+

I want to get the combo_name from combo table to reservation table and should look like this

+-------------+-----------+---------+------------+
|  rid        | r_date    | r_time  |combo_name  |
+-------------+-----------+---------+-----+------+
|   1         | 2019-12-20| 10:00:00| Package 1  |
|   2         | 2019-12-20| 10:00:00| Package 2  |
|   3         | 2019-12-20| 10:00:00| Package 3  |
+-------------+-----------+---------+-----+------+

this is my code pls help

$query = mysqli_query($con, "select * 
                             from reservation 
                             where  r_status='Approved' 
                               and r_date>='$today' 
                             order by r_date"
         ) or die(mysqli_error($con))`;
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • 2
    There is no obvious relationship between the 2 tables? – P.Salmon Nov 28 '19 at 10:41
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Dec 01 '19 at 12:42

4 Answers4

0

You can use join query :

$query = mysqli_query($con, "SELECT res.*, com.combo_name FROM reservation res join combo  com on res.rid = com.combo_id where  r_status='Approved' and r_date>='$today' ORDER BY  r_date"
         ) or die(mysqli_error($con))`;
Ramki
  • 452
  • 2
  • 16
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Dec 01 '19 at 12:42
0

Use with joins you will get it:

$query = mysqli_query($con, "select r.rid ,r.r_date,r. r_time ,c.combo_name 
                         from reservation r
                         join combo c on c.combo_id = r.rid
                         where  r_status='Approved' 
                           and r_date>='$today' 
                         order by r_date"
     ) or die(mysqli_error($con))`;
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Swetha Sekar
  • 237
  • 1
  • 9
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Dec 01 '19 at 12:42
0

anyway my problem was solved with this code thanks a lot to the suggestions

Select reservation.rid, reservation.r_time, reservation.r_last, combo.combo_name, reservation.r_first, reservation.r_date from reservation INNER JOIN combo on reservation.combo_id=combo.combo_id where r_status='Approved'
Dharman
  • 30,962
  • 25
  • 85
  • 135
-1

please try below query and let me know it it's not working for you.

$query = mysqli_query($con, "SELECT r.rid
    ,r.r_date
    ,r_time
    ,c.Combo_Name
FROM reservation AS r
LEFT JOIN combo AS c ON r.rid = c.combo_id
WHERE r.r_status = 'Approved'
    AND r.r_date >= CONVERT(VARCHAR, GETDATE(), 23)
ORDER BY r_date"
         ) or die(mysqli_error($con))`;
Ajeet Verma
  • 1,021
  • 1
  • 7
  • 25
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Dec 01 '19 at 12:41