2013-11-24 23 views
0

這是我構建的代碼, 請幫我修復我的代碼。加入相同書名的數量

$table="<tr> 
    <td>Book Title</td> 
     <td>Quantity</td> 
    </tr>"; 
$query = "select * from book"; 
$result = mysql_query($query); 

while($row = mysql_fetch_array($query)){ 
    $table.="<tr> 
      <td>{$row['title']}</td> 
      <td>{$row['quantity']}</td> 
      </tr>"; 
} 
$table="<table border>$table</table>"; 
echo $table; 
+0

什麼是你的問題,請再具體些。 – Kody

回答

0

Mysql已棄用,但這是另一個話題。

您可以在您的查詢與同一個標題來獲取書籍數量:

$table="<tr> 
     <td>Book Title</td> 
     <td>Quantity</td> 
    </tr>"; 
$query = "SELECT b.title, (SELECT COUNT(bc.title FROM book AS bc WHERE b.title=bc.title) AS quantity FROM book AS b"; 
$result = mysql_query($query); 

while($row = mysql_fetch_array($query)){ 
    $table.="<tr> 
      <td>{$row['title']}</td> 
      <td>{$row['quantity']}</td> 
      </tr>"; 
} 
$table="<table border>$table</table>"; 
echo $table;