获取 PHP 中 mySQL 行的值并回显某些行的多个值

Get values of mySQL rows in PHP and echo multiple values of certain rows

标题对我想做的事情有点含糊,所以我会在这里做解释。我有两个 table 可以从中获取信息,书籍 table 和库存 table。我从 Book table 中获取书名、作者姓名和 ISBN,并将 Inventory 中的数量和图书馆位置连接成一个。我想要的是信息看起来像

Title of Book
By Author1 Name and Author2 Name
ISBN: 740-fojsd99
(0) Copies available at Location2 
(3) Copies available at Location5

当我得到 php 中的回显信息时,它只得到清单的第一行 table 并且回显喜欢这个

22 11 63
By Stephen King
ISBN: 9788401344106
(0) Copies at the location of Sylvania Public Library

它应该看起来像

22 11 63
By Stephen King
ISBN: 9788401344106
(0) Copies at the location of Sylvania Public Library
(2) Copies at the location of Toledo Public Library
(1) Copies at the location of Carson Library

我尝试了分解、foreach 和使用两个单独的查询来获得我想要显示的内容,但我对 PHP 有点菜鸟,但仍在学习它是如何工作的。 我的php代码如下

$sql = "SELECT Book.Book_Title, 
Book.Author_Name, 
Book.Book_ISBN
FROM Book
GROUP BY Book_Title";


$result = mysqli_query($conn,$sql);


while($row = mysqli_fetch_assoc($result))
{   
    //var_dump($row);
    //had var_dump($result->num_rows); here
    echo "<b><u>". $row["Book_Title"] ."</u></b><br>";
    echo "By ". $row["Author_Name"]. "<br>";
    echo "ISBN: ". $row["Book_ISBN"] . "<br><br>";

    $ISBN = $row['Book_ISBN'];

    $sql2 = "SELECT CONCAT('<DD>(',Inventory.Quantity, ') Copies at the location of ', Inventory.Library_Location,'</DD>') as nCopies
    FROM Inventory
    JOIN Book
    ON Inventory.Book_Id=Book.Book_Id
    WHERE $ISBN = Book.Book_ISBN";

       $result2 = mysqli_query($conn,$sql2);

    while ($row = mysqli_fetch_assoc($result2)) 
    {
                echo "" . $row["numOfCopies"] . "<br>";
    var_dump($row);
        }

    var_dump($row);

} 

在我的数据库中这样做

SELECT Book.Book_Title, 
Book.Author_Name, 
Book.Book_ISBN
FROM Book
GROUP BY Book_Title

给我输出

+-----------------------------------------+-------------------------------------------+---------------+
| Book_Title                              | Author_Name                               | Book_ISBN     |
+-----------------------------------------+-------------------------------------------+---------------+
| 22 11 63                                | Stephen King                              | 9788401344106 |
| A Good Marriage                         | Stephen King                              | 9781401104428 |
| Bag of Bones                            | Stephen King                              | 9780671024239 |
| Carrie                                  | Stephen King                              | 9780307743664 |
| Cell                                    | Stephen King                              | 9781416424419 |
| Christine                               | Stephen King                              | 9780441160447 |
| Cujo                                    | Stephen King                              | 9780441161342 |
| Dark Souls: Design Works                | From Software                             | 9781926778891 |
| Desperation                             | Stephen King                              | 9781101137994 |
| Doctor Sleep                            | Stephen King                              | 9781441698844 |
| Dragon Age Inquisition: Official Guide  | David Knight                              | 9780804162944 |
| Duma Key                                | Stephen King                              | 9788401338090 |
| Everythings Eventual                    | Stephen King                              | 9780743447344 |
| Hearts in Atlantis                      | Stephen King                              | 9780671024246 |
| Heir to the Jedi: Star Wars             | Kevin Hearne                              | 9780344444848 |
| Insomnia                                | Stephen King                              | 9781101138007 |
| IT                                      | Stephen King                              | 9780441169418 |
| Joyland                                 | Stephen King, Hannes Riffel               | 9783641147074 |
| Mile 81                                 | Stephen King                              | 9781441664604 |
| Mr. Mercedes                            | Stephen King                              | 9781476744474 |
| Pet Sematary                            | Stephen King                              | 9780743412278 |
| Prince Caspian                          | C.S. Lewis, Pauline Baynes                | 9780064404003 |
| process control 221 Success Secrets     | David Knight                              | 9781488844672 |
| Secret Window                           | Stephen King                              | 9780441213469 |
| Skeleton Crew                           | Stephen King                              | 9780441168610 |
| Star Wars DarkSaber                     | Kevin J. Anderson                         | 9780307796417 |
| Star Wars I, Jedi                       | Michael A. Stackpole                      | 9780443478737 |
| Star Wars Mad Libs                      | Roger Price, Leonard Stern                | 9780843132717 |
| Star Wars Red Harvest                   | Joe Schreiber                             | 9780344418490 |
| Star Wars The Essential Atlas           | Jason Fry, Daniel Wallace                 | 9780344477644 |
| Star Wars: Choices of One               | Timothy Zahn                              | 9780344411263 |
| Star Wars: Dark Empire Trilogy          | Tom Veitch, Jim Baikie, Cam Kennedy       | 9781302466434 |
| Star Wars: Jedi Academy                 | Jeffery Brown                             | 9780444404178 |
| Star Wars: Rebel Force: Hostage         | Alex Wheeler                              | 9781484720226 |
| Star Wars: Riptide                      | Paul S. Kemp                              | 9780344422467 |
| Star Wars: Rise and Fall of Darth Vader | Ryder Windham                             | 9781484717874 |
| Tarkin: Star Wars                       | James Luceno                              | 9780344411422 |
| The Dead Zone                           | Stephen King                              | 9780441144747 |
| The Horse and His Boy                   | C.S. Lewis, Pauline Baynes                | 9780064471060 |
| The Last Battle                         | C.S. Lewis, Pauline Baynes, David Wiesner | 9780064404034 |
| The Legend of Zelda: Hyrule Historia    | Patrick Thorpe                            | 9781616440417 |
| The Lion, the Witch and the Wardrobe    | C.S. Lewis, Pauline Baynes                | 9780064471046 |
| The Magicians Nephew                    | C.S. Lewis, Pauline Baynes                | 9780064471107 |
| The Mist                                | Stephen King                              | 9780441223296 |
| The Running Man                         | Stephen King, Richard Bachman             | 9780441197962 |
| The Shining                             | Stephen King                              | 9780344806789 |
| The Stand                               | Stephen King                              | 9780307743688 |
| The Tommyknockers                       | Stephen King                              | 9780441146600 |
| Thinner                                 | Stephen King, Richard Bachman             | 9780441161344 |
| Under the Dome                          | Stephen King                              | 9781476734474 |
+-----------------------------------------+-------------------------------------------+---------------+

并正在做

SELECT CONCAT('<DD>(',Inventory.Quantity, ') Copies at the location of ', Inventory.Library_Location,'</DD>') as nCopies
            FROM Inventory
        JOIN Book
        ON Inventory.Book_Id=Book.Book_Id

(必须删除 GROUP BY Book_Title 因为它说组语句中的列 'Book_Id' 不明确)给了我结果

+----------------------------------------------------------------+
| nCopies                                                        |
+----------------------------------------------------------------+
| <DD>(0) Copies at the location of Sylvania Public Library</DD> |
| <DD>(1) Copies at the location of Carson Library</DD>          |
| <DD>(2) Copies at the location of Commuter Public Library</DD> |
| <DD>(0) Copies at the location of Sylvania Public Library</DD> |
| <DD>(2) Copies at the location of Toledo Public Library</DD>   |
| <DD>(1) Copies at the location of Carson Library</DD>          |
| <DD>(1) Copies at the location of Carson Library</DD>          |
| <DD>(2) Copies at the location of Commuter Public Library</DD> |
| <DD>(2) Copies at the location of Toledo Public Library</DD>   |
| <DD>(2) Copies at the location of Sylvania Public Library</DD> |
+----------------------------------------------------------------+

编辑:上次编辑 while 循环有效,结果返回如下

22 11 63
By Stephen King
ISBN: 9788401344106
(0) Copies at the location of Sylvania Public Library
(2) Copies at the location of Toledo Public Library
(1) Copies at the location of Carson Library
A Good Marriage
By Stephen King
ISBN: 9781401104428
(1) Copies at the location of Carson Library
(2) Copies at the location of Commuter Public Library
Bag of Bones
By Stephen King
ISBN: 9780671024239
(2) Copies at the location of Toledo Public Library
(2) Copies at the location of Sylvania Public Library
Cell
By Stephen King
ISBN: 9781416424419
(0) Copies at the location of Sylvania Public Library
(1) Copies at the location of Carson Library
(2) Copies at the location of Commuter Public Library

感谢@nomistic 的帮助,感激不尽!

你可以尝试这样的事情。基本上从您为特定书籍获得的结果中获取 a,然后循环访问这些位置应该可行。 (我知道它有点笨重,但它应该可以完成工作)。您遇到的上述问题是因为您只踢出第一行; PHP 除非您告诉它,否则不知道循环遍历初始查询的子部分。

while($row = mysqli_fetch_assoc($result))
    {   
        //had var_dump($row); here
        //had var_dump($result->num_rows); here
        echo "<b><u>". $row["Book_Title"] ."</u></b><br>";
        echo "By ". $row["Author_Name"]. "<br>";
        echo "ISBN: ". $row["Book_ISBN"] . "<br>";


        $ISBN = $row['Book_ISBN'];


        $sql2 = "SELECT CONCAT('<DD>(',Inventory.Quantity, ') Copies at the location of ', Inventory.Library_Location,'</DD>') as numOfCopies
        FROM Book
        JOIN Inventory
        ON Book.Book_Id=Inventory.Book_Id 
        WHERE Book.Book_ISBN = '$ISBN'
        GROUP BY Book_Title";
        $result2 = mysqli_query($conn,$sql2);

        while ($row = mysqli_fetch_assoc($result2)) {
            echo "" . $row["numOfCopies"] . "<br>";

        }

    }

请注意,您还可以清理第一个查询以仅提供第一轮所需的结果。

编辑:只是将 $ISBN 括在单引号中,因为我看到您将其视为字符串。 (虽然我不得不说在你的例子中这是一个非常奇怪的 ISBN;通常这些是数字,并且通常与连字符一起存储,而不是字母数字)