mysql_num_rows错误

时间:2009-07-25 12:29:27

标签: sql mysql

无法弄清问题是什么!

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


echo $msgs;

如果我删除“和read = 0”,上面的代码就有效。但如果不是我得到:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in left_menu.php on line 16

我在表中有一个字段叫 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);

问题是什么?

2 个答案:

答案 0 :(得分:1)

read是保留关键字,因此会生成MySQL语法错误。但是,在MySQL提示符下,这有效:

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

您必须在PHP查询中反引用它。

答案 1 :(得分:0)

@Jeremy说了什么,你可能想采用不同的方法。执行查询,检查错误,对结果执行其他操作(检查num行,读取数据等) 每个动作都在它自己的行中,而不是“Babushka链接”。