开发者

Php / Mysql While Loop Error

开发者 https://www.devze.com 2023-02-14 15:56 出处:网络
Okay, none of those responses I used but I fixed it myself. Im having trouble though trying to sum up all the prices for the books:

Okay, none of those responses I used but I fixed it myself.

Im having trouble though trying to sum up all the prices for the books:

Code:

<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 '</tab开发者_如何转开发le>';   //close out the table

?>


mysql_query returns FALSE on error, and you didn't check for this before trying to pass it to mysql_fetch_array.

You really should put error checking in your code!


Well maybe $result contains false because of error, add this block after mysql_query call.

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


Because your call $result = mysql_query($sql); has an error and returns false. You need to check for errors before you try to use a resource:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号