mysql_fetch_array() expects parameter 1 to be resource, boolean given

This is driving me crazy. I am trying to tech myself PHP and MySQL and have been doing Ok for only messing with it for a week, but for the last four days I have been trying to figure out what I am doing wrong.

I have a small inventory program I am making. I have two tables:

site_products and site_catagory

I am trying to make a drop down box with the categories listed, which are pulled from the site_catagory table, and onchange I would like the records containing that particular category shown.

Something is blowing up with my query (I think) but I am too much of a newbie to understand why. Would someone mind taking a look and seeing what the malfunction is?

Here is the page where I make the Menu, I named it test.php:

Select Catagory <?php mysql_connect('localhost','hesk','hesk'); mysql_select_db('inv'); $query=("SELECT * FROM site_catagory order by catagory_name ASC"); $result=mysql_query($query); while(list($id, $name)=mysql_fetch_row($result)) { echo "".$name.""; } ?>

And here is the page where it’s supposed to grab the records and populate the tables, I named it catagory.php

[php]if (is_numeric($_GET[‘catagory_id’]))
{
mysql_connect(‘localhost’,‘hesk’,‘hesk’);
mysql_select_db(‘inv’);
// $query=“SELECT * FROM site_products where id=$_GET[catagory_id]”;
$query=“SELECT product_id, product_name, product_number, product_count, product_description, product_vendor, product_catagory FROM site_products where id=$_GET[catagory_id]”;
$result=mysql_query($query);
while ($catagory=mysql_fetch_array($result))
{
echo "<table border=“1”>


ID:
“.$catagory[‘product_id’].”


Name:
“.$catagory[‘product_name’].”


Number:
“.$catagory[‘product_number’].”


Count:
“.$catagory[‘product_count’].”


Description:
“.$catagory[‘product_description’].”


Vendor:
“.$catagory[‘product_vendor’].”


Catagory:
“.$catagory[‘product_catagory’].”

";
}
}[/php]

If someone can help me figure out what I am doing wrong, I would really appreciate it!!

So what part of code is not working - are you able to see drop down list of categories and select one? What error message you get?

The error I get is:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\inv\forms\admin\products\catagory.php on line [b]9

[/b]Sorry, I guess I should have included that.The menu works, but when a menu item is chosen and the query is run to grab the recods, I get the above error.

In your query you have this condition:

where id=$_GET[catagory_id]

Is the field “id” contain category id? Probably correct field is “product_catagory”?

I changed all instances of catagory_id to product catagory and I still get the same error:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\inv\forms\admin\products\catagory.php on line 9

I am really stumped…

There was no need to change all instances. I just pointed to field that looked more relevant to your query. But I can’t guess what you table fields are. For debugging purposes, try to eliminate “where” clause from your query.
So that this line in your code:

[php]
$query=“SELECT product_id, product_name, product_number, product_count, product_description, product_vendor, product_catagory FROM site_products where id=$_GET[catagory_id]”;
[/php]

Will be replaced with this:
[php]
$query=“SELECT * FROM site_products”;
[/php]

You should get list of all products. If this worked, you can go further… Check what is the field name in site_products table where category ID actually stored (and also what is field type - numeric or text), and add “where” clause to your query using this field.

That worked, it listed everything in site_products, but lists the Category wrong:

[table][tr][td]ID:[/td] [td]1[/td] [/tr] [tr] [td]Name:[/td] [td]USB Flash Drive (4GB)[/td] [/tr] [tr] [td]Number:[/td] [td]FQ433AA #ABA[/td] [/tr] [tr] [td]Count:[/td] [td]934[/td] [/tr] [tr] [td]Description:[/td] [td]USB 4GB Flash Drive[/td] [/tr] [tr] [td]Vendor:[/td] [td]1[/td] [/tr] [tr][td] Catagory: 2
[/td][/tr][/table]
The site_catagory table contains catagory_id and catagory_name, which I used to populate the drop down menu.

I have a link on the intranet webpage which allows me or my boss to enter a new category type into the site_catagory table with two columns:

catagory_id and catagory_name

Right now the site_products table stores the catagory ID, but on the drop down menu I would like to list the categories … like LCD Monitors, and have it show all the LCD Monitors in the database.

Great. Now we know correct field name :slight_smile:
So, your query should look like this:

[php]
$query=“SELECT site_products.*, catagory_name FROM site_products left join site_catagory on site_products.product_catagory=site_catagory.catagory_id where product_catagory=”.((int)$_GET[‘catagory_id’]);
[/php]

Also, replace this line:
[php]

“.$catagory[‘product_catagory’].”
[/php]

to this:
[php]

“.$catagory[‘catagory_name’].”
[/php]

If you have a Paypal account, please let me buy you a beer :slight_smile: Thank you for taking the time to help me out, I really appreciate it! And thanks to the code, I can learn how to fix this up and hopefully help other people out once I get more proficient.

Thanks again!

(And I am serious about the beer, ha ha!)

Beer is always welcome :wink: but I don’t use Paypal.
No problem!

Sponsor our Newsletter | Privacy Policy | Terms of Service