使用MySQLi求和的单个结果 [英] Single Result From SUM With MySQLi
问题描述
环顾四周.找不到答案. PHP文档尚不清楚(对我来说).
Looked all over. Can't find an answer. PHP docs not clear (to me).
我正在通过mysqli->query
做一个简单的MySQL求和.我如何像mysql_result
一样使用MySQLi获得结果?
I'm doing a simple MySQL sum through mysqli->query
. How can I get the result with MySQLi like mysql_result
?
推荐答案
(提意见以回答)
如果查询成功,则SELECT语句中给mysqli_query的任何内容都将返回mysql_result类型.因此,如果您有SELECT语句,例如:
Whatever is given in the SELECT statement to mysqli_query is going to return a mysql_result type if the query was successful. So if you have a SELECT statement such as:
SELECT sum(field) FROM table1
您仍然需要使用结果获取行,sum()函数的值将是行数组中的唯一条目:
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:
$res = mysqli_query($dbh,'SELECT sum(field) FROM table1');
if (FALSE === $res) die("Select sum failed: ".mysqli_error);
$row = mysqli_fetch_row($res);
$sum = $row[0];
*请记住检查mysqli_query的返回值,以确保查询不会失败.*
*Remember to check the return from mysqli_query to ensure the query did not fail.*
这篇关于使用MySQLi求和的单个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!