0

Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given

I am new in PHP, need some advice on the following code. I get the following error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\wd\categories.php on line 42

PHP Code:

$SQL= "FROM  wdccat WHERE caid='$id' 
LEFT JOIN wdcat ON wdccat.caid = wdcat.ccid
LEFT JOIN wdclient ON wdccat.clid = wdclient.cid";
$result = mysql_query($SQL);

((Line 42)) while ($db_field = mysql_fetch_assoc($result)) {

echo $str1;
echo $str2;
print $row['cid'];
echo $str3;
print $row['cid'];
echo $str4;
print $row['climage'];
echo $str5;
print $row['cname'];
echo $str6;
print $row['cid'];
echo $str7;
print $row['cname'];
echo $str8;
print $row['cid'];
echo $str9;
echo $str10;

}

Any assistance will be appreciated.

The error i get with echo mysql_error();:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN wdcat ON wdccat.caid = wdcat.ccid LEFT JOIN wdclient ON wdccat.cl' at line 2 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\wd\categories.php on line 42

Community
  • 1
  • 1
Boldie
  • 25
  • 1
  • 12

3 Answers3

2

You should check the result of mysql_query() for errors (returns false), eg

$result = mysql_query($SQL);
if (false === $result) {
    throw new Exception('Error in SQL query, you have: ' . mysql_error());
}

In your case, the query is malformed; You are missing the SELECT clause.

Some further advice; switch to the PDO library ASAP and start using parameterised statements with bound parameters. Further reading here - http://forums.whirlpool.net.au/forum-replies.cfm?t=1234522

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Exactly. When running SQL code, there are two things you can't assume: "your query will succeed" and "input parameters will never contain single quotes". – Álvaro González Nov 15 '11 at 10:25
0

Try trowing a echo mysql_error() because theres something wrong with your query.

Probably because you are missing SELECT *

Niels
  • 48,601
  • 4
  • 62
  • 81
-1

Your $SQL is incomplete. It doesn't have the returned fields. Try:

$SQL= "SELECT wdclient.cid, climage, wdclient.cname FROM  wdccat WHERE caid='$id' 
LEFT JOIN wdcat ON wdccat.caid = wdcat.ccid
LEFT JOIN wdclient ON wdccat.clid = wdclient.cid";

This is assuming that cid, climage, cname comes from the same table.

CristiC
  • 22,068
  • 12
  • 57
  • 89