1

I'm using the following query to display some information:

$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY count DESC ");

My issue is it works fine when I leave out ORDER BY count DESC but when it is there I get the following error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /proj/co600/project/repo/public_html/select_field3.php on line 227

Count is a column in my database which records the number of times a publication is downloaded.

Air
  • 8,274
  • 2
  • 53
  • 88
jonny
  • 7
  • 5
  • 2
    Include more code. Your error message refers to mysqli_fetch_array() which doesn't appear here. – Mike Sherrill 'Cat Recall' Mar 28 '14 at 15:48
  • 1
    you should use `mysqli_query (..) or die(mysqli_error())` to get the proper error messages do not avoid them – M Khalid Junaid Mar 28 '14 at 15:55
  • Do you have a `count` column in more than one of the tables you are querying? – Brian Driscoll Mar 28 '14 at 15:56
  • My guess, without seeing more code, is that you have a `count` column in more than one table and thus the reference in your `order by` clause is ambiguous. – Brian Driscoll Mar 28 '14 at 16:25
  • @BrianDriscoll THAT WAS THE PROBLEM I HAD TWO COUNT COLOUMS ! thank you guys for your help i really appreciate it – jonny Mar 28 '14 at 16:31
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Apr 08 '14 at 02:56

2 Answers2

4

count is an aggregate function, so you need to surround it with backticks.

To get a clear cut picture of your error.. You need to change your code like..

$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY count DESC ");
if(!$result)
{
 die(mysqli_error($con));
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • @CodingAnt, I don't understand too :( – Shankar Narayana Damodaran Mar 28 '14 at 15:48
  • @ShankarDamodaran `count` is not a reserved keyword [mysql docs](https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html) says clearly **COUNT is acceptable as a column name** and in the keyword list count is not mentioned – M Khalid Junaid Mar 28 '14 at 15:52
  • By the way, I'm not sure this is really the problem, see http://sqlfiddle.com/#!2/78a39/1 . In fact, count is NOT a reserved keyword (sorry for that, it's a BUILT-IN FUNCTION). And CAN be used as a column name... – Raphaël Althaus Mar 28 '14 at 15:53
  • @ShankarDamodaran that edit is meaning full and i guess user should be curious to use columns with their table alias this will overcome the problem of have same column name in multiple tables although +1 for the edit – M Khalid Junaid Mar 28 '14 at 16:01
-1

You are having MySQL reserved keyword as column name in table.

Use Below query:

$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY `count` DESC ");
Elixir Techne
  • 1,848
  • 15
  • 20