Php功能在PDO中不起作用

时间:2012-05-27 15:03:29

标签: php function pdo

我在旧的mysql中创建了一个函数,现在想将它转移到PDO,但是它无法正常工作。这是我的新代码:

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, school_students 
    WHERE (school_test_report.student_id = school_students.student_id 
    and school_test_report.class=school_students.class) 
    and school_test_report.student_id = '$student_id' and  school_test_report.subject = '$subject'
    and school_test_report.test_date >= '$thisarch'
                                ")
    $STH->setFetchMode(PDO::FETCH_ASSOC);
    return $STH;

并输出:

$student_id = test_score_month($name, 'English');
echo $student_id['score'].'/'.$student_id['score_from'];

以下是旧代码:

$result1 = mysql_query("SELECT SUM(score), SUM(score_from) FROM school_test_report, school_students 
    WHERE (school_test_report.student_id = school_students.student_id 
    and school_test_report.class=school_students.class) 
    and school_test_report.student_id = '$student_id' and  school_test_report.subject = '$subject'
    and school_test_report.test_date >= '$thisarch'
                                ")
    or die(mysql_error());  
    $row = mysql_fetch_assoc($result1);
 return $row;

并输出:

$student_id = test_score_month($name, 'English');
echo $student_id['score'].'/'.$student_id['score_from'];

1 个答案:

答案 0 :(得分:1)

Heres是一个示例,将您的所有函数包装在与模型类中的用户相关的查询中,然后调用每个方法返回结果ect,看看函数/方法如何使用占位符:student_id然后bindParam的占位符的价值,这只是一个例子,但会帮助你更多地了解abit。

<?php 
class user_model{

    private $db;

    function __construct($host,$dbname,$user,$pass){
        $this->dbhost = $host;
        $this->dbname = $dbname;
        $this->dbuser = $user;
        $this->dbpass = $pass;
    }

    private function connect(){
        if (!$this->db instanceof PDO){
            $this->db = new PDO('mysql:dbname='.$this->dbname.';host='.$this->dbhost, $this->dbuser, $this->dbpass);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
    }

    function user_test_score($student_id,$subject,$thisarch){
        $this->connect();
        $sql = "SELECT SUM(score) as score, SUM(score_from) FROM school_test_report, school_students
                WHERE (school_test_report.student_id = school_students.student_id 
                AND school_test_report.class=school_students.class) 
                AND school_test_report.student_id = :student_id and  school_test_report.subject = :subject
                AND school_test_report.test_date >= :thisarch";
        $statement = $this->db->prepare($sql);
        $statement->bindParam(':student_id', $student_id, PDO::PARAM_INT);
        $statement->bindParam(':subject', $subject, PDO::PARAM_STR);
        $statement->bindParam(':thisarch', $thisarch, PDO::PARAM_STR);
        $statement->execute();
        return $statement->fetchAll(PDO::FETCH_ASSOC);
    }

}


$usermodel = new user_model('localhost','YOURDB','username','password');

$student_id = $usermodel->user_test_score($name,'English',your_test_date_format);

print_r($student_id);
?>