0

I have two tables 1) live_booking table 2) company_car table

the company car PK is car_id. there is a field in the booking table with the car_id for a user to input.

The issue is that when the user runs the SQL query it doesnt return any results, if there is no car_id in the booking table, However if the car_id present into the booking table by the user then the SQL query shows all the results.

I would like it that if there is NO car_id in the booking table then the query gets the remaining fields from the booking table and shows the car fields as blank.

$bookingid = $_GET['id']; 

$result = mysqli_query($con, "
SELECT 

live_booking.booking_id, live_booking.booking_time, live_booking.booking_date, live_booking.id_mobile,live_booking.reservation_time, live_booking.car_id, 

company_cars.car_driver, company_cars.car_make, company_cars.car_model,

customer.eu_name


FROM live_booking live_booking
inner join customer customer on customer.id_mobile = live_booking.id_mobile

inner join company_cars company_cars on company_cars.car_id = live_booking.car_id

WHERE booking_id = $bookingid");
user3247335
  • 181
  • 2
  • 5
  • 16
  • remove the sql server tag,your php suggests mysql. – Mihai Apr 18 '14 at 18:27
  • INNER JOIN looks for records that are common between two databases. You probably want a LEFT JOIN. See http://stackoverflow.com/questions/5706437/whats-the-difference-between-inner-join-left-join-right-join-and-full-join for more information. LEFT JOIN will give you all records from your first table, and then join in the fields from the extra tables. – DragonYen Apr 18 '14 at 18:28
  • Why do you linking the tables with a car_id AND a id_mobile? – Nick Apr 18 '14 at 18:31

2 Answers2

0

you can use mysqli_num_rows and if statement here if No result then show rest of all results

like

     $row_cnt = $result->num_rows;
        if ($row_cnt == 0)

        {
        do this 
        }
       else 
        {
       do this
        }

for more information check this http://www.php.net/manual/en/mysqli-result.num-rows.php

plus choice right tags :)

M.chaudhry
  • 651
  • 1
  • 6
  • 13
0

I only needed to use a Left Join instead of inner join. http://www.w3schools.com/sql/sql_join_left.asp

user3247335
  • 181
  • 2
  • 5
  • 16