Cómo solucionarlo Advertencia: mysql_fetch_array () espera que el parámetro 1 sea un recurso, ¿booleano dado? [duplicar]

Posible duplicado:
PHP: Advertencia: sort () espera que el parámetro 1 sea una matriz, recurso proporcionado

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';
    } 

}

preguntado el 10 de marzo de 12 a las 17:03

Tus 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 evalúa a true and if not, show or log the output of mysql_error. -

@AnotherCode although you are right in general, mysql error messages are quite vague and can help a little -

6 Respuestas

Esto significa mysql_query failed to execute the query, and you should use mysql_error() para ver el mensaje de error.

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.)

respondido 10 mar '12, 17:03

+1 ... and not PDO yet, PDO is not good for beginners. He must learn some mysql basics first :) - Wh1T3h4Ck5

thanks for the link i'll go and check it out - jimmyv

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

respondido 10 mar '12, 17:03

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

NUNCA assume a query has succeeded.

respondido 10 mar '12, 17:03

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

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");

debería ser:

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

respondido 10 mar '12, 17:03

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

Prueba esto:

$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';
    } 
}

respondido 10 mar '12, 17:03

Remove quotes from numerics.

Tier=1

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

respondido 10 mar '12, 17:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.