mysql_result()期望参数1是资源,布尔给定[重复]

mysql_result()期望参数1是资源,布尔给定[重复]

问题描述:

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.

可能重复: strong>
警告:mysql_fetch_array()期望参数1为 资源,布尔值在 p> blockquote>

我认为标题说明了一切。 p>

这是我的代码: p>

  function user_count(){
 $ query =“SELECT COUNT * FROM users WHERE active = 1”  ; 
 $ result = mysql_query($ query); 
返回mysql_result($ result,0); 
} 
  code>  pre> 
 
 

使用此功能。 我正试图从数据库中获取所有“活跃”的用户...... 数据库连接和那些东西都没问题。 p> div>

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.

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

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