mysql_fetch_array() error in simple shopping cart page

I am learning how to build a shopping cart using PHP and MySQL. I am testing a basic script that builds the products page. I am getting the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in products.php on line 41

This is line 41:

while($row = mysql_fetch_array($result))

When I run the SQL Query (select * from items order by itemName asc) in PHPMyAdmin I get the desired results. I am not sure what could be causing the problem. Please help!

Here is the PRODUCTS.PHP code:

[php]<?php
// This page will list all of the items
// from the items table. Each item will have
// a link to add it to the cart
include(“db.php”);
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$result = mysql_query(“select * from items order by itemName asc”);
?>

Product List

Products

<?php while($row = mysql_fetch_array($result)) { ?> <?php } ?>
Product Price Description Add
<?php echo $row["itemName"]; ?> $ <?php echo $row["itemPrice"]; ?> <?php echo $row["itemDesc"]; ?> &qty=1">Add Item

Your Shopping Cart >>
[/php]

Thank you.

Just after your query, try echoing the mysql_error() and see what it says, it may give you more of a clue of what is wrong.

Also consider removing the @ sign on the connection to see if there is an error there. The @ sign will suppress any error messages.

Good luck.

Thank you! I had the wrong database name assigned to $dbName. DUH!! :o

Sponsor our Newsletter | Privacy Policy | Terms of Service