This is a beginner php/mysql question, I’ve messed around for hours with various loops but I haven’t been able to reach the goal. I have two tables in the db. One that contains books and one that contain the stock info for the books. Like this:
id title
1 A book
2 Another book
3 A third book
and for the stock (book_stock)
id book_id warehouse_id qty
1 1 1 12
2 1 2 45
3 2 3 22
4 3 1 78
5 1 3 15
I want to read the tables, match the id from book table with book_id from stock table and then output a summary in a html table, showing book titles and how many of each book exist in the different warehouses. What I’ve tried so far involves looping mysql requests, something that seems to crash since it outputs no results. I have a feeling the solutions is simple and involves arrays but my skills are lacking…
Here is my non-working code. The function works fine for 1, 2 and even 20 results entered manually, but only outputs blank table cells when looped.
//function to get stock based on book id number
function get_stock($book_row_number)
{
//get BOOK from db
$book_query="SELECT * FROM book";
$book_result=mysql_query($book_query);
//determine book info
$book_id=mysql_result($book_result,$book_row_number,"id");
$book_title=mysql_result($book_result,$book_row_number,"title");
//get LONDON stock for this book from db
$stock_query="SELECT qty FROM book_stock WHERE book_id='$book_id' AND warehouse_id='1'";
$stock_result=mysql_query($stock_query);
$stock_london=mysql_result($stock_result,0,"qty");
//get USA stock for this book from db
$stock_query="SELECT qty FROM book_stock WHERE book_id='$book_id' AND warehouse_id='2'";
$stock_result=mysql_query($stock_query);
$stock_usa=mysql_result($stock_result,0,"qty");
//get GERMANY stock for this book from db
$stock_query="SELECT qty FROM book_stock WHERE book_id='$book_id' AND warehouse_id='4'";
$stock_result=mysql_query($stock_query);
$stock_germany=mysql_result($stock_result,0,"qty");
echo "<tr><td>$book_title</td><td>$stock_london</td><td>$stock_usa</td><td>$stock_germany</td></tr>\n";
}
//find number of rows in book list
$all_query="SELECT * FROM book";
$all_result=mysql_query($all_query);
$all_rows=mysql_numrows($all_result);
// run the function on all the rows
$i=0;
while ($i < $all_rows) {
get_stock('$all_rows');
$i++;
}
EDIT: And the code as it is now
<?php
//set up connection
$user="username";
$password="paswd";
$database="database";
mysql_connect("localhost",$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
//query
$book_query="SELECT
book_id,
title,
SUM(CASE WHEN warehouse_id = 1 THEN book_stock.qty ELSE 0 END) AS warehouse1,
SUM(CASE WHEN warehouse_id = 2 THEN book_stock.qty ELSE 0 END) AS warehouse2,
SUM(CASE WHEN warehouse_id = 3 THEN book_stock.qty ELSE 0 END) AS warehouse3,
SUM(CASE WHEN warehouse_id = 4 THEN book_stock.qty ELSE 0 END) AS warehouse4
FROM
book_stock
JOIN book ON book.id = book_stock.book_id
GROUP BY book_id, title";
//loop
$result = mysql_query($book_query);
if ($result) {
while ($row = mysql_fetch_assoc($row)) {
echo "<tr><td>" . htmlspecialchars($row['title']) . "</td><td>{$row['warehouse1']}</td><td>{$row['warehouse2']}</td><td>{$row['warehouse3']}</td><td>{$row['warehouse4']}</td></tr>";
}
}
else echo mysql_error();
//close database
mysql_close();
?>
You can do this in a single query, if you build it as a pivot table. The idea is that the
SUM()aggregates add together the value ofqtyfor the matchingwarehouse_id, or 0 when it doesn’t match for each row, collapsing all the warehouses into a single row.For
book_id = 1, this produces a result like:To get it for all books in rows, add a
GROUP BY:Output from PHP is then a trivial
whileloop: