0

Possible Duplicate:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

I think the title says everything.

Here's my code:

function user_count(){
    $query = "SELECT COUNT * FROM  users  WHERE  active = 1";
    $result = mysql_query($query);
    return mysql_result($result, 0);
}

With this function. I'm trying to get all the users from the database who are "active"... All is OK with db connection and that stuff.

Community
  • 1
  • 1
gtboy
  • 136
  • 1
  • 3
  • 12
  • 2
    Please stop writing new code with the ancient MySQL extension: it is no longer maintained and the community has begun the [deprecation process](http://news.php.net/php.internals/53799); you can use instead either the improved [MySQLi](http://php.net/mysqli) extension or the [PDO](http://php.net/pdo) abstraction layer. – eggyal Jun 06 '12 at 10:51

3 Answers3

1

Your query is wrong. Try replacing with below one

$query = "SELECT COUNT(*) FROM  users  WHERE  active = 1";

You should have some mysql error handling implemented to avoid such type of warnings.

Suggestion: Better to use PDO to talk your database

Shakti Singh
  • 81,083
  • 20
  • 131
  • 150
1

In mysql count is a function so you should use COUNT(*) and not COUNT *

You should also consider tracking errors with functions like mysql_error when the mysql_query function returns false and not a resource

also, the common side note: think about switching to PDO

mishu
  • 5,297
  • 1
  • 24
  • 38
0
function user_count(){
    $query = "SELECT COUNT(user_id) FROM  `users` WHERE  `active` = 1";
    $result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
    return mysql_result($result, 0);
}

This works.

gtboy
  • 136
  • 1
  • 3
  • 12