我构建了以下查询并在其中使用了MySQL的 COUNT函数。
查询是:
SELECT COUNT(`id`) FROM `users` WHERE `gender` = 'Female' AND `sub_by` = 'Self' AND `country` = 'Pakistan' AND `religion` = 'Christian' AND `ma_status` = 'Single' AND `occupation` = 'Unemployed'
稍后在我的脚本中我使用PHP的函数 mysql_result()和 mysql_fetch_assoc ,并且它们都给我错误消息,这意味着问题不在于mysql_result ()和mysql_fetch_assoc(),但问题出在查询中,
错误消息是:
警告:mysql_result()期望参数1为资源,第76行的C:\ wamp \ www \ practice3 \ search2.php中给出布尔值
警告:mysql_fetch_assoc()期望参数1为资源,第87行的C:\ wamp \ www \ practice3 \ search2.php中给出布尔值
任何人都可以告诉我查询中的真正问题是什么。
答案 0 :(得分:0)
mysql_result()
返回false
不要使用'\'
正确的查询是
SELECT COUNT(`id`)
FROM `users` WHERE `gender` = 'Female'
AND `sub_by` = 'Self'
AND `country` = 'Pakistan'
AND `religion` = 'Christian'
AND `ma_status` = 'Single'
AND `occupation` = 'Unemployed'
答案 1 :(得分:0)
SELECT COUNT(\`id\`) FROM \`users\` WHERE \`gender\` = 'Female' AND \`sub_by\` = 'Self' AND \`country\` = 'Pakistan' AND \`religion\` = 'Christian' AND \`ma_status\` = 'Single' AND \`occupation\` = 'Unemployed'
should be
SELECT COUNT(`id`) FROM `users` WHERE `gender` = 'Female' AND `sub_by` = 'Self' AND `country` = 'Pakistan' AND `religion` = 'Christian' AND `ma_status` = 'Single' AND `occupation` = 'Unemployed'
答案 2 :(得分:0)
使用以下查询替换您的查询
SELECT COUNT(`id`) FROM `users` WHERE `gender` = 'Female' AND `sub_by` = 'Self' AND `country` = 'Pakistan' AND `religion` = 'Christian' AND `ma_status` = 'Single' AND `occupation` = 'Unemployed'
这是您的新查询,这将为您提供所需的确切结果。
在PHP字符串中使用单引号或双引号时使用反斜杠,但在使用撇号(`)时永远不能使用。