-1

Possible Duplicate:
PHP: Warning: sort() expects parameter 1 to be array, resource given

I keep getting the following message Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\test.class.php on line 22 But when i check the code i can't see what is wrong...

<?php
// Main menu items
$sql=mysql_query("SELECT * FROM category WHERE tier='1' ORDER BY id ASC");
while($row = mysql_fetch_array($sql)){      
    $p_id= $row["id"];
    $p_category_name= $row["category_name"];
    $mainMenu[''.$p_category_name.'']= $p_id;

    $sql_1=mysql_query("SELECT * FROM category WHERE tier='2' parent='$p_id' ORDER BY id ASC");
    while($row_1 = mysql_fetch_array($sql_1)){ **This is line 22**
        $c_id= $row_1["id"];
        $c_category_name= $row_1["category_name"];
        $subMenu[''.$p_category_name.''][''.$c_category_name.''] = 'product1.php';
    } 

}
Community
  • 1
  • 1
JimmyV
  • 59
  • 1
  • 1
  • 7
  • Your `mysql_query` is probably generating a MySQL error, but you don't seem to have any error handling code. Try adding some first, for example by checking if the return value of `mysql_query` evaluates to `true` and if not, show or log the output of `mysql_error`. – Another Code Mar 10 '12 at 17:04
  • @AnotherCode although you are right in general, mysql error messages are quite vague and can help a little – Your Common Sense Mar 10 '12 at 17:13

6 Answers6

3

You have an error in your query (forgot the and). Try this

SELECT * FROM category WHERE tier='2' and parent='$p_id' ORDER BY id ASC
juergen d
  • 195,137
  • 36
  • 275
  • 343
3

This means mysql_query failed to execute the query, and you should use mysql_error() to see the error message.

In this case, it looks like you're missing an AND:

$sql_1=mysql_query("SELECT * FROM category WHERE tier='2' AND parent='$p_id' ORDER BY id ASC");

(And please look into PDO.)

DCoder
  • 12,740
  • 4
  • 42
  • 61
2

You've got an SQL syntax error:

$sql_1=mysql_query("SELECT * FROM category WHERE tier='2' parent='$p_id' ORDER BY id ASC");
                                                         ^-- missing ' AND '

If you had proper error handling in your code, you've have seen this:

$sql_1 = mysql_query(...) or trigger_error(mysql_error());
                         ^^^^^^^^^^^^^^^^^^^^^^--- bare mininum handling

NEVER assume a query has succeeded.

Your Common Sense
  • 154,967
  • 38
  • 205
  • 325
Marc B
  • 348,685
  • 41
  • 398
  • 480
  • Thanks for the quick responce. I am new to all this and i lack a lot of knowledge, i will try to keep that in my mind – JimmyV Mar 10 '12 at 17:09
1

AND is missing from the query in the WHERE clause

    $sql_1=mysql_query("SELECT * FROM category WHERE tier='2'  parent='$p_id' ORDER BY id ASC");

it should be:

    $sql_1=mysql_query("SELECT * FROM category WHERE tier='2' AND parent='$p_id' ORDER BY id ASC");
Junaid Qadir Shekhanzai
  • 1,335
  • 1
  • 19
  • 38
0

Try making $row an array:

while($row[] = mysql_fetch_array($sql))

make sure all your queries are like this:

$sql = mysql_query("SELECT * FROM category WHERE tier='1' ORDER BY id ASC") or die(mysql_error());

http://php.net/manual/en/function.mysql-fetch-array.php

Additionally I think your supposed to be using mysql_fetch_assoc(); http://www.php.net/manual/en/function.mysql-fetch-assoc.php

Try this:

$sql = "SELECT * FROM category WHERE tier='1' ORDER BY id ASC";
$fetch = mysql_fetch_assoc(mysql_query($sql)) or die(mysql_error());
while ($row = $fetch) {
    $p_id = $row["id"];
    $p_category_name = $row["category_name"];
    $mainMenu[''.$p_category_name.'']= $p_id;

    $sql_1 = "SELECT * FROM category WHERE tier='2' AND parent='$p_id' ORDER BY id ASC";
    $fetch_1 = mysql_fetch_assoc(mysql_query($sql_1)) or die(mysql_error());
    while ($row_1 = $fetch_1) {
        $c_id = $row_1["id"];
        $c_category_name = $row_1["category_name"];
        $subMenu[''.$p_category_name.''][''.$c_category_name.''] = 'product1.php';
    } 
}
GaminGrounds
  • 161
  • 1
  • 3
-4

Remove quotes from numerics.

Tier=1

plus check your sql by running in phpmyadmin to check whether it runs or not.

itachi
  • 6,072
  • 3
  • 29
  • 40