-1

I build the following query and used MySQL's COUNT function in it.

The query is :

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'

Than later in my script I used PHP's functions mysql_result() and mysql_fetch_assoc, and both of them give me error messages, which means the problem is not with the mysql_result() and mysql_fetch_assoc(), but the problem is in the query,

The error messages are:

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\practice3\search2.php on line 76

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\practice3\search2.php on line 87

Can anyone tell me what the real problem is in my query.

Community
  • 1
  • 1
Bangash
  • 1,152
  • 3
  • 10
  • 30
  • 1
    Don't escape your backtick – Fabio Jun 14 '13 at 10:49
  • Don't use `mysql_` functions, use `mysqli_...` or PDO. Without seeing the code, I would say, establishing the DB connection is what fails - therefore you don't pass a connection resource but the instead returned boolean `FALSE` to `_result` and `_fetch_assoc`. If you get an error message, you need to actually read it, you know?! – Tom Regner Jun 14 '13 at 10:53
  • If you called `mysql_error()` after `mysql_result()` failed, you would see a syntax error or other problem reported in your query. Without seeing more of your PHP code, we can't really assist much. – Michael Berkowski Jun 14 '13 at 10:53

3 Answers3

0

mysql_result() return false

do not use '\'

The right query is

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'
Strawberry
  • 33,750
  • 13
  • 40
  • 57
  • Thank You for your answer, but I didn't use \ in my query I used them only to skip backtick in here, i didn't use them in my real query. – Bangash Jun 14 '13 at 11:38
  • Dears, I got the answer to my question, Actually the query and all the script was OK, the real problem was that I have once used single quote instead of backtick in my query which was giving the error... Thank You all for your answers..! – Bangash Jun 14 '13 at 11:44
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'
Goutam Pal
  • 1,763
  • 1
  • 10
  • 14
  • Thank You for your answer, but I didn't use \ in my query I used them only to skip backtick in here, i didn't use them in my real query... – Bangash Jun 14 '13 at 11:41
0

Replace you query with the following Query

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'

this is your new query and this will provide you the exact result you want.

backslash is used when you are using single quotes or double quotes in PHP string, but can never be used when you are using apostrophe (`).

  • Thank You for your answer, but I didn't use \ in my query I used them only to skip backtick in here, i didn't use them in my real query.. – Bangash Jun 14 '13 at 11:42
  • 1
    then as i think that there is error in your connection file and one thing that you need to know that count query provide only single row result. so i want you to use mysql_fetch_row(); – Mudasir Nazir Jun 14 '13 at 11:51