Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中显示SUM列_Php_Mysql - Fatal编程技术网

如何在PHP中显示SUM列

如何在PHP中显示SUM列,php,mysql,Php,Mysql,我一直在尝试用PHP中的SUM()函数显示一列的摘要 我使用的是$pointsummary=“从结果中选择SUM(points),其中id=$val”调用我的SUM函数,但我无法在PHP中显示它。我尝试了$test=mysqli\u fetch\u数组($pointsummary)然后echo$test[0]但它不起作用。 当我这样做时,我得到: mysqli_fetch_array()希望参数1是mysqli_结果 我该怎么办?您的错误是因为$pointsummary是字符串而不是mysqli

我一直在尝试用PHP中的SUM()函数显示一列的摘要

我使用的是
$pointsummary=“从结果中选择SUM(points),其中id=$val”
调用我的SUM函数,但我无法在PHP中显示它。我尝试了
$test=mysqli\u fetch\u数组($pointsummary)
然后echo
$test[0]但它不起作用。
当我这样做时,我得到:

mysqli_fetch_array()希望参数1是mysqli_结果


我该怎么办?

您的错误是因为
$pointsummary
是字符串而不是mysqli\u结果对象。您需要先使用,然后使用返回的内容。例如

$resultObj = mysqli_query($con, $pointsummary); // <--- $con is what you got from mysqli_connect()
$resultArr = mysqli_fetch_array($resultObj);

@Memor-X的答案很好,但我感觉在使用
mysqli\uuucode>PHP查询数据库时,您至少错过了正常事件流中的一个步骤

// create a query as a text string
$pointsummary = "SELECT SUM(points) as `tot_points` 
                 FROM result 
                 WHERE id=$val";

// issue the query to the MySQL Server for execution
// this should create a result set as long as the query is valid
$result = mysqli_query($con, $pointsummary);

// Check that the query had no errors
if ( ! $result ) {
    echo mysqli_error($con);
    exit;
}

// request the first row from the result set
// as we know this query will only return one row
// we dont need to do this in a loop
$row = mysqli_fetch_assoc($result);

echo $row['tot_points'];

嗨,Horam Zraki,MySQL中的点是什么类型的字段?@aharen其编号。。我想得到的结果是,把它们加起来,意思是专栏的“类型”,而不是“内容”<代码>整数
<代码>变量字符
?其他?@Fred ii-对不起,这是你的
积分
列,是你数据库中的第一列,还是第二列?数组是零索引的。这意味着,如果
id
是您的第一列,那么您将尝试获取
id
的索引,而不是
points
。因此,您需要执行
$test[1]
if
points
是第二列。Memor-X是最好的答案。但我建议您添加几行示例代码。我不认为提问者知道他漏掉了
$result=mysqli\u查询($con,$pointsummary)一步到位我想特工是去做爆米花,然后去看电影什么的,甚至可能会在一家秘密的汽车旅馆见面,谁知道呢。自从几天前发表评论以来,他们就没有任何消息。问题还不清楚,就我而言,我们不知道他们是否使用相同的API来连接等。
// create a query as a text string
$pointsummary = "SELECT SUM(points) as `tot_points` 
                 FROM result 
                 WHERE id=$val";

// issue the query to the MySQL Server for execution
// this should create a result set as long as the query is valid
$result = mysqli_query($con, $pointsummary);

// Check that the query had no errors
if ( ! $result ) {
    echo mysqli_error($con);
    exit;
}

// request the first row from the result set
// as we know this query will only return one row
// we dont need to do this in a loop
$row = mysqli_fetch_assoc($result);

echo $row['tot_points'];