且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PDO PHP连接,致命错误

更新时间:2023-02-22 17:16:31

您快到了.在您的课程中,您需要使用以下命令更改$db的每次迭代:

You're almost there. In your class, you need to change each iteration of $db with:

$this->db

所以您的课程看起来像这样:

So your class would look like this:

class DB_functions {
    public $db;
    function __construct() {
        try{
            $this->db = new PDO("mysql:localhost;dbname=xxx;charset=utf8","xxx","xxx");
            echo 'Connected';
        }catch(PDOException $e){
            print $e->getMessage();
            echo "No Connection";
        }
    }
    function __destruct() {}

    public function test(){

        $query = $this->db->query("SELECT * FROM User", PDO::FETCH_ASSOC);
        if($query->rowCount()){
            foreach ($query as $row) {
                print_r($row);
            }
        }
    }

}

在引用内部类变量时.它只能在类范围内访问,并通过$this引用.