1

可能重复:
为什么这会返回 Resource id #2?

我想回显mysql_query("SELECT SUM(onlineplayers) FROM servers"),但是当我将回显放在前面时,它显示资源 ID #2,当我or die(mysql_error());在最后添加时,它只输出 1。

4

4 回答 4

2

You need to fetch the query first:

$result = mysql_query("SELECT SUM(onlineplayers) FROM servers");
if($result){
  $data = mysql_fetch_assoc($result);
  echo $data[0];
}

However, you shouldn't use the mysql_ functions unless absolutely necessary. the mysql extension is NOT recommended for use in new projects. Instead you should use PDO_mysql or mysqli

Source: Why does this return Resource id #2?

于 2013-02-02T06:39:00.623 回答
1

use this below code

$str = "SELECT SUM(onlineplayers) FROM servers";  //this will set the query in string format
echo $str;    // this will echo the query;
mysql_query($str);   // this will run the query
于 2013-02-02T06:34:30.057 回答
1
$q = mysql_query("SELECT SUM(onlineplayers) as `total` FROM servers"); // notice the "as `total`
$r = mysql_fetch_array($q); // will return the result
echo $r['total']; // will echo the count

On a sidenote, please stop using mysql_* functions. More info here

于 2013-02-02T06:39:16.500 回答
0
$str = "SELECT SUM(onlineplayers) FROM servers"; 
echo $str; 
$result = mysql_query($str); 
$row= mysql_fetch_array($result);
print_r($row);
于 2013-02-02T06:41:28.623 回答