I'm facing a problem when trying to select all the orders of a given client acessing the website and display them on the screen trhough a table. The problem is that the array that is being returned has the values of the orders tripled (each order appears 3 times and not one).
$query = "SELECT orders.orderID, orders.date, products.name, products.price
FROM ((orders
INNER JOIN customers ON orders.userID = ?)
INNER JOIN products ON orders.productID = products.productID)";
$stm = $pdo->prepare($query);
$stm->execute([$_SESSION['id']]); // The ID of the customer using the website
$orders_array = $stm->fetchAll(PDO::FETCH_ASSOC);
My database has a customers table, a products table and an orders table. I wanted the associative array to have the order id, product name, order date, and price, and in fact, all of this is being returned. However, it is being selected 3 times for each order. I've searched on the internet for examples like this, but I could not find any examples that fit what I want. Does anyone have any idea why the values are tripled
Your JOIN
is not correct. The ON
clause needs to relate columns in two tables.
Since you're not selecting anyting from the customers
table, you don't need that in the query at all.
SELECT orders.orderID, orders.date, products.name, products.price
FROM orders
INNER JOIN products ON orders.productID = products.productID
WHERE orders.userID = ?
if you did want to include customer information, it would be:
SELECT customer.name, orders.orderID, orders.date, products.name, products.price
FROM orders
INNER JOIN customer ON order.userID = customer.userID
INNER JOIN products ON orders.productID = products.productID
WHERE customer.userID = ?
SELECT * FROM Customer ORDER BY Nama_Customer;
SELECT * FROM Customer ORDER BY Nama_Customer DESC;
SELECT * FROM Pasok ORDER BY Jumlah_Pasok;
SELECT * FROM Pasok ORDER BY Jumlah_Pasok DESC;
SELECT * FROM Supplier WHERE Nama_Supplier NOT LIKE '%tron';
SELECT * FROM Customer WHERE Kode_Customer='J-001' OR Kode_Customer='B-002';
SELECT * FROM Customer WHERE Kode_Customer IN ('J-001','B-002');
SELECT Nama_Customer FROM Customer WHERE Kode_Customer IN ('J-001','B-002') ORDER BY Nama_Customer;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.