3

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

nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Carla Dessi
  • 9,086
  • 9
  • 39
  • 53
  • 2
    http://stackoverflow.com/search?q=%E2%80%9CWarning%3A%20mysql_fetch_array()%20expects%20parameter%201%20to%20be%20resource%2C%20boolean%20given%E2%80%9D%20 - 5000+ questions. Please do some research. – Mat Feb 28 '12 at 12:35

2 Answers2

2

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());
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • might as well escape each word and form a good habit :) – Moak Feb 28 '12 at 12:36
  • @CarlaDessi: Sorry my fault, I misspelled that function name it is `trigger_error()`. Kindly change it. – Shakti Singh Feb 28 '12 at 12:39
  • Change of plan, now it says Notice: SQL error:No database selected in /Applications/XAMPP/xamppfiles/htdocs/cart/product.php on line 13 – Carla Dessi Feb 28 '12 at 12:40
  • @CarlaDessi: Oh! select the database first using. [mysql_select_db](http://php.net/manual/en/function.mysql-select-db.php). I recommend to further read the PHP mysql examples. – Shakti Singh Feb 28 '12 at 12:45
0

@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();
 }
Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I used that, then got this error: Notice: SQL error:No database selected in /Applications/XAMPP/xamppfiles/htdocs/cart/product.php on line 13 – Carla Dessi Feb 28 '12 at 12:44
  • @Carla that's your problem then. Are you selecting a database when establishing the connection? – Pekka Feb 28 '12 at 12:44
  • yeh.. the fields come up fine in the header for a different query, just not the ones on the product page. – Carla Dessi Feb 28 '12 at 12:46