0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I have this:

$query=mysql_query("SELECT DISTINCT username FROM messages 
WHERE to='admin' AND read='0'");

If I use mysql_fetch_assoc($query) I get the error:

PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given

However, when I remove the following from the SQL statement: AND read='0', there is no error and everything works.

There is something about that "read" column. Its type is "int(1)" with default value 0

Does anyone know why it's doing that? THanks a lot

Community
  • 1
  • 1
alexx0186
  • 1,385
  • 5
  • 18
  • 32
  • 2
    Please stop writing new code with the ancient `mysql_*` functions. They are no longer maintained and community has begun the [deprecation process](http://news.php.net/php.internals/53799) . Instead you should learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you care to learn, [here is a quite good PDO-related tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – tereško May 01 '12 at 19:22
  • Hi thanks very much for your links. I'm going through that PDO tutorial. Regards – alexx0186 May 01 '12 at 21:12

2 Answers2

4

Read is a reserved keyword in MySQL.

You need to enclose the column name with backticks:

AND `read`='0'
nickb
  • 58,150
  • 12
  • 100
  • 138
1

Add backticks (`) around your column names as read is a reserved word in MySQL.

SenorAmor
  • 3,321
  • 15
  • 27