-1

我构建了以下查询并在其中使用了 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 中给出

谁能告诉我查询中真正的问题是什么。

4

3 回答 3

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 字符串中使用单引号或双引号时使用,但在使用撇号 (`) 时永远不能使用。

于 2013-06-14T10:59:57.523 回答
0

mysql_result()返回假

不使用 '\'

正确的查询是

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'
于 2013-06-14T10:54:23.203 回答
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'
于 2013-06-14T10:55:01.363 回答