1

I am trying to run 2 SQL Select queries to retrieve data from 2 different tables and then echo the fields from both tables. The code I am trying doesn't seem to work, any help would be appreciated.

$ModelID = $_GET['model_id'];

$result = mysqli_query($con, "SELECT RegNumber, Colour FROM Car WHERE ModelID = '$ModelID'

UNION ALL

SELECT CarModel, CarMake, CostPerDay FROM Model WHERE ModelID = '$ModelID'");   

while($row = $result->fetch_assoc())    
{     
    echo $row["CarModel"];
    echo $row["CarMake"];
    echo $row["CostPerDay"];    
    echo $row["RegNumber"];        
    echo " - " .$row["Colour"];         
}                       

2 Answers2

1

You can change request :

SELECT c.RegNumber, c.Colour , m.CarModel, m.CarMake, m.CostPerDay FROM Car c INNER JOIN Model m ON m.ModelID=c.ModelID WHERE c.ModelID = '$ModelID'
Fky
  • 2,133
  • 1
  • 15
  • 23
1

try with LEFT JOIN

$result = mysqli_query($con, 
   "SELECT c.RegNumber, c.Colour, m.CarModel, m.CarMake, m.CostPerDay 
   FROM Car AS c
   LEFT JOIN Model as m ON c.ModelID = m.ModelID 
   WHERE c.ModelID = '".$ModelID."'"
);