0

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

I get this error:

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

$sql = dbquery("SELECT * FROM `channels` WHERE `cat_slug` = ".$cat." ");
while($row = mysql_fetch_array($sql)){
$category =  $row["cat_name"];
$slug =  $row["cat_slug"];
// other
}

and $cat can be for example "funny"

how can i change the code to get it done?

Community
  • 1
  • 1
m3tsys
  • 3,859
  • 6
  • 27
  • 43

2 Answers2

1

You're using PEAR so you'll want to set up the object first:

$db =& DB::connect('mysql://usr:pw@localhost/dbname');
if (PEAR::isError($db)) {
    die($db->getMessage());
}

then create a resource:

$res =& $db->query("SELECT * FROM `channels` WHERE `cat_slug` = '$cat'");

Then you can fetch an array if your fetchmod is set to ordered:

while ($res->fetchInto($row)) {
    echo $row[0] . "\n";
}

It looks like you were using a mixture of standard PHP and PEAR.

in standard PHP, you'd need to do it like this:

$sql = "SELECT * FROM channels WHERE cat_slug = '$cat'";
$res = mysql_query($sql, $conn) //where $conn is your db link stuff

THEN you could do a fetch array

Jim Wharton
  • 1,331
  • 2
  • 17
  • 38
  • yes the last one was the problem. It works with `$sql = dbquery("SELECT * FROM channels WHERE cat_slug = '$cat' ");` Thank you. – m3tsys Jun 28 '11 at 18:49
0

You have a syntax error in the query, and/or something else wrong with the database. Within your dbquery function, you'd need to have something like:

$result = mysql_query($sql) or (die(mysql_error());

which will abort the script and output the reason why the query failed.

However, given your query string, and your data going into it, the error is due to a lack of quotes around your $cat within the query:

SELECT ... WHERE `cat_slug`=funny;

Unless your table has a field called "funny", this is a syntax error. You need:

SELECT ... WHERE `cat_slug`='funny';

(note the quotes).

Marc B
  • 348,685
  • 41
  • 398
  • 480