[英]Single Result From SUM With MySQLi
Looked all over.都看了一遍。 Can't find an answer.
找不到答案。 PHP docs not clear (to me).
PHP 文档不清楚(对我来说)。
I'm doing a simple MySQL sum through mysqli->query
.我正在通过
mysqli->query
做一个简单的 MySQL 总和。 How can I get the result with MySQLi like mysql_result
?如何使用 MySQLi 获得结果,例如
mysql_result
?
It's best if you used an alias for your SUM:最好为 SUM 使用别名:
SELECT SUM(`field`) as `sum` FROM `table_name`
And then, you'll be able to fetch the result normally by accessing the first result row's $row['sum']
.然后,您将能够通过访问第一个结果行的
$row['sum']
正常获取结果。
Whatever is given in the SELECT statement to mysqli_query is going to return a mysql_result type if the query was successful.如果查询成功,SELECT 语句中给 mysqli_query 的任何内容都将返回 mysql_result 类型。 So if you have a SELECT statement such as:
因此,如果您有一个 SELECT 语句,例如:
SELECT sum(field) FROM table1
you still need to fetch the row with the result, and the value of the sum() function will be the only entry in the row array:您仍然需要获取带有结果的行,并且 sum() 函数的值将是行数组中的唯一条目:
$res = mysqli_query($dbh,'SELECT sum(field) FROM table1');
$row = mysqli_fetch_row($res);
$sum = $row[0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.