English 中文(简体)
Php 不在 PDO 中工作的 PP 函数
原标题:Php Function not working in 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 ];
最佳回答

这里举一个例子, 将所有您要处理的功能与您在模型类中的用户相关查询包起来, 然后调用每种方法返回您的输出, 查看函数/ 方法如何使用占位符 < code> : student_ id : student_ id , 然后将值绑定Param, 将值绑定到 Thos 占位符中, 这只是个示例, 但有助于您理解比特更多 。

<?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);
?>
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签