Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given
This is the code I have in my product.php
, every time I click on a product from the home page it comes up with the warning below:
if ( isset( $_GET['ID'] ) ) {
$product_id = $_GET['ID'];
$query = "SELECT Name, Genre, Price, Year, Picture FROM Products";
$result = mysql_query( $query );
while ( $row = mysql_fetch_array( $result, MYSQL_NUM ) ) {
echo "<div><p>Name: $row[0]</p><p>Genre: $row[1]</p><p>Price: $row[2]</p><p>Year: $row[3]</p></div>";
}
echo "<div><a href=\"cart.php?action=add&product=$product_id\">add to basket</a></div>";
}
and I get the warning:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/cart/product.php on line 12
Year
is a mysql reserved word you have to escape it using backticks ``
$query = "SELECT Name, Genre, Price, `Year`, Picture FROM Products";
you must use some kind of mysql error checking like below one
$result = mysql_query($query) or trigger_error(mysql_error());
@Click already pointed out your error.
More generally though - you are not doing any error checking in your query, so it's no wonder you're not getting any useful information when the query fails. How to do this is outlined in the manual on mysql_query()
or in this reference question.
Example:
$result = mysql_query($query);
if (!$result)
{ trigger_error("SQL error:".mysql_error());
die();
}