I am trying retrieve multiple rows from multiple tables but I think I am not doing it in the right way. The project is kind of a shop online, I have 3 tables in it: orders, orderdetails and services, which all are linked with an ID:
I have Order ID and Service ID in orderdetails' table, it means I inserts a row for each item on the basket linked to Service ID to see which service is, and Order Id to check for which order are. Example:
services table
-
service_id|name   |price
------------------------
2         |Tech   |100
------------------------
4         |Support|150
------------------------
10        |Mainten|50
------------------------
orders table
-
order_id|customer_id|name|lastname
----------------------------------
10      |16         |John|Smith
----------------------------------
orderdetails table
-
orderdetails_id|order_id|service_id|price|quantity
--------------------------------------------------
1              |10      |2         |100  |4
--------------------------------------------------
2              |10      |4         |150  |2
--------------------------------------------------
3              |10      |10        |50   |1
--------------------------------------------------
I inserts service's price on orderdetails table because maybe the services price can change AFTER a customer order it.
At this moment I have this query:
$query = $this->db->prepare(
'SELECT orders.*, orderdetails.*, services.*
FROM  orders
LEFT JOIN orderdetails
ON orderdetails.order_id = orders.order_id
LEFT JOIN services
ON orderdetails.service_id = services.service_id
WHERE orders.order_id = ?
AND orders.customer_id = ?');
And I got this result:
    stdClass Object
(
    [order_id] => 10
    [customer_id] => 16
    [name] => Tech
    [lastname] => Smith
    [orderdetails_id] => 1
    [service_id] => 2
    [price] => 100
    [quantity] => 4
)
stdClass Object
(
    [order_id] => 10
    [customer_id] => 16
    [name] => Support
    [lastname] => Smith
    [orderdetails_id] => 2
    [service_id] => 4
    [price] => 150
    [quantity] => 2
)
stdClass Object
(
    [order_id] => 10
    [customer_id] => 16
    [name] => Mainten
    [lastname] => Smith
    [orderdetails_id] => 3
    [service_id] => 10
    [price] => 50
    [quantity] => 1
)
I have two problems. The 1st problem is I have the same column name in orders table and services table. The 2nd is the query returns all the information (because I know I am not querying well), but I expect to receive something like this:
stdClass Object
(
    [order_id] => 10
    [customer_id] => 16
    [name] => John
    [lastname] => Smith
    [orderdetails_id] => 1
    [service_id] => 10
    [price] => 50
    [quantity] => 1
    [service_name] => Mainten
    [orderdetails_id2] => 2
    [service_id2] => 4
    [price2] => 150
    [quantity2] => 2
    [service_name2] => Support
    [orderdetails_id3] => 3
    [service_id3] => 2
    [price3] => 100
    [quantity3] => 4
    [service_name3] => Tech
)
I mean, I am not an expert in SQL Queries, and I read a lot, but I think you guys could help me to figure it out this because I have other two tables to link with: customer-service-worker who will get the order to process, and area's table who will receive the order.
I use this code for getting the objects:
$array = array(); 
while($loop = $result->fetch_object()){ $array[] = $loop; }
return $array;
 
    