Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.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_Mysql_Sql - Fatal编程技术网

Php mysql中计算列和时出错

Php mysql中计算列和时出错,php,mysql,sql,Php,Mysql,Sql,表tblstationerystock中有一列名为stationeryqtyrecd。我想打印特定数量(如笔)的stationeryqtyrecd的总值。但我的查询显示错误。我的查询为 <?php include('includes/config.php'); $sql = " SELECT SUM(stationeryqtyrecd) as total from tblstationerystock where stationerytype = :type &q

表tblstationerystock中有一列名为stationeryqtyrecd。我想打印特定数量(如笔)的stationeryqtyrecd的总值。但我的查询显示错误。我的查询为

  <?php
include('includes/config.php');

$sql = "
SELECT SUM(stationeryqtyrecd) as total 
  from tblstationerystock 
 where stationerytype = :type
"; 
$query = $dbh -> prepare($sql);
$query->bindParam(':type', "pen", PDO::PARAM_STR);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$total=$results->total();



echo $total;

?>

试试这个:

include('includes/config.php');

$type='pen';
$sql = "SELECT SUM(stationeryqtyrecd) as total from tblstationerystock where stationerytype = :type"; 
$query = $dbh -> prepare($sql);
$query->bindParam(':type', $type);
$query->execute();
if($row = $query->fetch(PDO::FETCH_OBJ)) {
    print($row->total);
}

您绑定的参数isread不在query@juergend我在代码中更正了它仍然不工作什么错误?请看@jameson2012你能解释或写完整的代码吗?我从total中删除了括号,现在它有错误通知:试图在第10行的C:\xampp\htdocs\sms\admin\test.php中获取非object的属性“total”,我已经测试了这段代码,它工作正常。如果您没有显示任何数据和php错误,那么您的sql查询很可能有问题。我建议您进行一些调试,并检查所有的列名
include('includes/config.php');

$type='pen';
$sql = "SELECT SUM(stationeryqtyrecd) as total from tblstationerystock where stationerytype = :type"; 
$query = $dbh -> prepare($sql);
$query->bindParam(':type', $type);
$query->execute();
if($row = $query->fetch(PDO::FETCH_OBJ)) {
    print($row->total);
}