gpt4 book ai didi

带有 PDO 函数的 Php Sum 和 mysql

转载 作者:行者123 更新时间:2023-11-29 02:32:37 24 4
gpt4 key购买 nike

PDO 的新手...我正在尝试求和,但这里出现错误是我的代码

function test_score_month($student_id){
global $host, $dbname, $user, $pass;

$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$STH = $DBH->query("SELECT SUM(score), SUM(score_from) FROM school_test_report
WHERE
school_test_report.test_date >= last_march() and school_test_report.test_date <= march()" );
$STH->setFetchMode(PDO::FETCH_BOTH);
return $STH;
}

输出是

$student_id = test_score_month($name);
echo $student_id['SUM(score)'];

但出现错误

Call to a member function setFetchMode() on a non-object in

我试过了

$STH->setFetchMode(PDO::FETCH_ASSOC);

还是一样的错误

最佳答案

您的查询有语法错误:

WHERE 
and

您还确定您的数据库中有 march()last_march() 函数吗?

[+] 总是在查询数据库后检查错误:

if($STH){
// do your stuff
}else{
die($DBH->errorInfo()); // see the error
}

参见 http://www.php.net/manual/en/pdo.errorinfo.php

关于带有 PDO 函数的 Php Sum 和 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com