I have the following 3 tables that I'm trying to join using a single query to read the data. The tables are users, vehicles and operators. The main table holding all the relationships is vehicles
users
id | name
1 | Driver 1
2 | Driver 2
3 | Conductor 1
4 | Conductor 2
5 | Owner 1
6 | Owner 2
vehicles
id | reg_num|driver_id | conductor_id | owner_id | operator_id
1 | KAN100X | 2 | 4 | 6 | 2
2 | KCN200Y | 1 | 3 | 5 | 1
operators
id | name | type
1 | Operator 1 | Intercity
2 | Operator 2 | Intracity
I have tried this
class Vehicle{
private $conn;
// object properties
public $id;
public $name;
public $reg_num;
public $driver_id;
public $conductor_id;
public $owner_id;
public $operator_id;
public $type;
function read(){
$query = "
SELECT * FROM vehicles
LEFT JOIN users ON 'users.id' = 'vehicles.owner_id'
LEFT JOIN users ON 'users.id' = 'vehicles.driver_id'
LEFT JOIN users ON 'users.id = vehicles.coductor_id'
LEFT JOIN users ON 'operators.id = vehicles.operator_id'
ORDER BY
'vehicles.id'
DESC";
// prepare query statement
$stmt = $this->conn->prepare($query);
// execute query
$stmt->execute();
return $stmt;
}
}
// instantiate database and vehicle object
$database = new Database();
$db = $database->getConnection();
// initialize object
$vehicle = new Vehicle($db);
// query vehicle
$stmt = $vehicle->read();
$num = $stmt->rowCount();
// check if more than 0 record found
if($num>0){
// vehicle array
$vehicle_arr=array();
$vehicle_arr["vehicle"]=array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$vehicle_item = array(
"vehicle_id" => $id,
"vehicle_reg_num" => html_entity_decode($reg_num),
"operator_name" => html_entity_decode($name),
"operator_type" => html_entity_decode($type),
"driver_name" => html_entity_decode($driver_name),
"owner_name" => html_entity_decode($owner_name),
"conductor_name" => html_entity_decode($conductor_name)
);
array_push($vehicle_arr["vehicle"], $vehicle_item);
}
// set response code - 200 OK
http_response_code(200);
// show vehicle data in json format
echo json_encode(
array("vehicle" => $vehicle_arr["vehicle"])
);
}
else{
// set response code - 404 Not found
http_response_code(404);
// tell the user no vehicle found
echo json_encode(
array("response_code" => 6, "response_message" => "No vehicle found.")
);
}
How could i rewrite the query to return results?