you have to make it with another subquery as a second alias.
I tried making something like this…
$sql = "
SELECT id, product, image,
(SELECT SUM(nitem) FROM order_items WHERE order_items.product_id = products.id) AS nitem,
(SELECT SUM(total) FROM order_items WHERE order_items.product_id = products.id) AS total,
FROM products
ORDER BY nitem DESC
";```
But that didn't work. I'm not sure how to make that double selection. Read a few things, but are outside my scope. (even though I did tried, fiddling with it.) like the CASE for instance, but at the end still not sure where I'm going wrong.
I tried using &, &&, |, || the word AND, also tried putting the statements into () parenthesis but still not sure how to make it select 2 values from the modified query.
you should tell mysqli to always throw exceptions on syntax errors, just in case you may mistakenly have a comma after your last coulmn selection.
1 Like
Hello chron,
doe that mean that it should work? Also, how do I make mysqli throw this verbose output. I’m new to the programming world. I fiddle here and there as I learn. I’m a designer (2D/3D) at core. But make a living mostly 2D.
Thank you!
Here is the current SQL and it display everything as it should:
<table width="96%" border="0" align="center">
<tr>
<td>ID3</td>
<td width="90px">Image</td>
<td>Name</td>
<td align="right">Sold</td>
<td align="right">Gross</td>
</tr>
<?
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "
SELECT id, product, image,
(SELECT SUM(nitem) FROM order_items WHERE order_items.product_id = products.id) AS nitem,
(SELECT SUM(total) FROM order_items WHERE order_items.product_id = products.id) AS total
FROM products
ORDER BY nitem DESC
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$report_id = $row["id"];
$report_image = $row["image"];
$report_product = $row["product"];
$report_nitem = $english_format_number = number_format($row["nitem"]);
$report_total = $english_format_number = number_format($row["total"], 2);
echo "
<tr>
<td>$report_id</td>
<td height=\"90px\"><img src=\"../shop/$report_image\" width=\"80px\" height=\"auto\"></td>
<td>$report_product</td>
<td align=\"right\">$report_nitem</td>
<td align=\"right\">$$report_total</td>
</tr>
";
}
}
$conn->close();
?>
</table>
Thank you for all your help. Thanks to you I have made the code cleaner.
Best Regards!
2 Likes
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.