Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:55:11+00:00 2026-06-10T17:55:11+00:00

This is a beginner php/mysql question, I’ve messed around for hours with various loops

  • 0

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();
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:55:12+00:00Added an answer on June 10, 2026 at 5:55 pm

    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 of qty for the matching warehouse_id, or 0 when it doesn’t match for each row, collapsing all the warehouses into a single row.

    SELECT
      SUM(CASE WHEN warehouse_id = 1 THEN qty ELSE 0 END) AS warehouse1,
      SUM(CASE WHEN warehouse_id = 2 THEN qty ELSE 0 END) AS warehouse2,
      SUM(CASE WHEN warehouse_id = 3 THEN qty ELSE 0 END) AS warehouse3,
      SUM(CASE WHEN warehouse_id = 4 THEN qty ELSE 0 END) AS warehouse4
    FROM
      book_stock
    WHERE book_id = $book_id
    

    For book_id = 1, this produces a result like:

    warehouse1 warehouse2 warehouse3 warehouse4
           12        45          15          0
    

    To get it for all books in rows, add a GROUP BY:

    SELECT
      book_id,
      title,
      SUM(CASE WHEN warehouse_id = 1 THEN qty ELSE 0 END) AS warehouse1,
      SUM(CASE WHEN warehouse_id = 2 THEN qty ELSE 0 END) AS warehouse2,
      SUM(CASE WHEN warehouse_id = 3 THEN qty ELSE 0 END) AS warehouse3,
      SUM(CASE WHEN warehouse_id = 4 THEN qty ELSE 0 END) AS warehouse4
    FROM
      book_stock
      JOIN book ON book.id = book_stock.book_id
    GROUP BY book_id, title
    

    Output from PHP is then a trivial while loop:

    // Assuming you already opened your <table>
    $result = mysql_query($the_big_query_above);
    if ($result) {
      while ($row = mysql_fetch_assoc($result)) {
        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();
    
    // Then don't forget to close your </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner in PHP and SQL. Maybe this is a silly question,
This question gets asked a lot, but I still haven't found a straight answer
I'm no php expert (a mere beginner) but need some help! After hours searching
I am a beginner at PHP, but have been attempting a project on a
I am a PHP beginner and saw on the forum this PHP expression: My
I am beginner in PHP. I am trying to parse this xml file. <relationship>
This is a topic that, as a beginner to PHP and programming, sort of
This is a beginner-best-practice question in perl. I'm new to this language. The question
This is a beginner level question for asp.net MVC I have the following code
I believe this is a beginner's CSS question. I am utilizing the method described

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.