2011-03-02 141 views
-2

好吧,我沒有使用這些回覆,但我自己修復了它。雖然試圖總結所有的價格爲圖書Php/Mysql雖然循環錯誤

有麻煩林:

代碼:

<style type="text/css"> 
table{font-size:1.11em;} 
tr{background-color:#eee; border-top:1px solid #333;} 
</style> 
<?php 
$con = mysql_connect("localhost","root","pass"); 
if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("bookorama", $con); 

$sql="SELECT customers.name, books.title, books.isbn, books.price 
     FROM customers, orders, order_items, books 
     WHERE customers.customerID = orders.customerID 
     AND orders.orderID = order_items.orderID 
     AND order_items.isbn = books.isbn;"; 

$result = mysql_query($sql);  // You actually have to execute the $sql with mysql_query(); 
if($result === FALSE) { 
    die(mysql_error()); // TODO: better error handling 
} 
echo "<h1 style='color:#3366ff;'>Each customer book orders</h1>"; 
echo "<table>"; //start the table 

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) //Loop through the results 
{ 
    //echo each row of the table 
    echo "<tr>";    
    echo "<th><strong>Customer Name:</strong><br></th>";    
    echo "<td>$row[name]</td>";  
    echo "<th><strong>Book Title</strong><br></th>";     
    echo "<td>$row[title]</td>"; 
    echo "<th><strong>ISBN</strong><br></th>"; 
    echo "<td>$row[isbn]</td>"; 
    echo "<th><strong>Book Price</strong><br></th>"; 
    echo "<td>$row[price]</td>"; 
    echo "</tr>"; 
} 

echo '</table>'; //close out the table 

?> 

回答

0

嗯,也許$結果中包含錯誤,因爲虛假的,請求mysql_query調用後添加此塊。

 
if (!$result) { 
    die('Invalid query: ' . mysql_error()); 
} 
1

mysql_queryreturns誤差FALSE,而你沒有試圖將它傳遞給mysql_fetch_array之前檢查這一點。

你真的應該在你的代碼中進行錯誤檢查!

0

因爲您的電話$result = mysql_query($sql);有錯誤,並且返回false。在嘗試使用資源之前,您需要檢查錯誤:

$result = mysql_query($sql); 
if(!$result) { 
    echo "SQL Query failed! " . mysql_error(); 
} 
else { 
    // rest of your code here 
}