tags:

views:

40

answers:

2

Cant figure out what the problem is!

$msgs = mysql_num_rows(mysql_query("SELECT * FROM messages WHERE recipient = $userID AND read = 0"));


echo $msgs;

The above code works if I remove "and read =0". But if not i get:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in left_menu.php on line 16
. I have a field in the table called read (tinyint 1,defualt 0)
CREATE TABLE IF NOT EXISTS `messages` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`sender` int(10) unsigned NOT NULL DEFAULT '0',
`recipient` int(10) unsigned NOT NULL DEFAULT '0',
`subject` varchar(255) CHARACTER SET utf8 NOT NULL,
`message` text CHARACTER SET utf8,
`date` int(10) unsigned NOT NULL DEFAULT '0',
`read` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)

INSERT INTO `messages` (`id`, `sender`, `recipient`, `subject`, `message`, `date`, `read`) VALUES
(1, 47, 13, 'Hello!', 'TEST!', 1228326055, 0),
(2, 536, 13, 'blblabla', 'yeah', 1248506708, 0);

Whats the problem?

+1  A: 

read is a reserved keyword, so this generates a MySQL syntax error. However, at the MySQL prompt, this works:

SELECT * FROM messages WHERE recipient = 1 AND `read` = 0;

You'll have to backquote it in your PHP query.

Jeremy Smyth
wow didnt know that changed the name on the field. thanks!
A: 

What @Jeremy said, and you might want to adopt a different approach. Perform the query, check for errors, do other stuff on result (check num rows, read data etc).
Each action in it's own line, not "Babushka chained".

Itay Moav