Php mysql中计算列和时出错
表tblstationerystock中有一列名为stationeryqtyrecd。我想打印特定数量(如笔)的stationeryqtyrecd的总值。但我的查询显示错误。我的查询为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
<?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);
}