可能重复:
为什么这会返回 Resource id #2?
我想回显mysql_query("SELECT SUM(onlineplayers) FROM servers")
,但是当我将回显放在前面时,它显示资源 ID #2,当我or die(mysql_error());
在最后添加时,它只输出 1。
可能重复:
为什么这会返回 Resource id #2?
我想回显mysql_query("SELECT SUM(onlineplayers) FROM servers")
,但是当我将回显放在前面时,它显示资源 ID #2,当我or die(mysql_error());
在最后添加时,它只输出 1。
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
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
$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
$str = "SELECT SUM(onlineplayers) FROM servers";
echo $str;
$result = mysql_query($str);
$row= mysql_fetch_array($result);
print_r($row);