2

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);

但仍然是同样的错误

4

1 回答 1

2

您的查询中有语法错误:

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

于 2012-05-27T11:11:51.287 回答