Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 如何从mysql数据库添加数组值?_Php_Arrays - Fatal编程技术网

Php 如何从mysql数据库添加数组值?

Php 如何从mysql数据库添加数组值?,php,arrays,Php,Arrays,我想添加基于客户名称的产品数量。在这里,我获取基于客户名称的值并存储到数组中,现在我想添加数量值。在select查询中,如果客户在两行中分别有2+2=4个数量,我如何添加数量值 $selectproduct = "SELECT * FROM purchase_item WHERE custname = '$customername'"; $resultselectproduct = $conn->query($selectproduct); if ( $resultselect

我想添加基于客户名称的产品数量。在这里,我获取基于客户名称的值并存储到数组中,现在我想添加数量值。在select查询中,如果客户在两行中分别有2+2=4个数量,我如何添加数量值

 $selectproduct = "SELECT * FROM purchase_item WHERE custname = '$customername'";  
 $resultselectproduct = $conn->query($selectproduct); 
 if ( $resultselectproduct ->num_rows >0 )
 {
     while($rowselectproduct = $resultselectproduct->fetch_assoc())
     { 
          $array[] = $rowselectproduct;
     }
}
我的数据库结构:

custname  product  qty
 A          ProA    2
 A          ProB    2

当我基于“A”客户运行查询时,我得到的值是数量值4

只要更改查询并使用SUM()函数即可。像这样:

$query = "SELECT 

SUM(qty) AS total

FROM `purchase_item`

WHERE `custname` = '$customername'"; 

$stmt = $conn->query($query);

$result = $stmt->fetch_assoc();

echo $result['total'];

你的问题不清楚。显示您想要的输出显示您的数据库结构哪种糟糕的数据库设计通过用户名识别记录?这应该是一个id。我更新了我的问题。但是我得到了一个错误致命的错误:不能在第40行的C:\wamp\www\onceagaincome.php中使用mysqli\u result类型的对象作为数组