标题对我想要做的事情有点模糊,所以我在这里做解释。我有两个表,我从Book表和Inventory表中获取信息。我从Book表中获取了Book Title,Author Name和ISBN,并将Inventory中的Quantity和Library Location连接成一个。我想要的是信息看起来像
Title of Book
By Author1 Name and Author2 Name
ISBN: 740-fojsd99
(0) Copies available at Location2
(3) Copies available at Location5
当我在php中获取echo信息时,它只获取Inventory表的第一行,echo就像这样
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,因为它说Group&#39; Book_Id&#39;在群组声明中含糊不清)给了我结果
+----------------------------------------------------------------+
| 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的帮助,谢谢!
答案 0 :(得分:1)
你可以尝试这样的事情。基本上从特定书籍的结果中获取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;通常这些是数字的,通常用连字符存储,但不是字母数字)