PHP MYSQL PDO 数据库连接

PHP MYSQL PDO Database Connection

我正在使用 PHP 使用 PDO 建立到 MYSQL 数据库的连接。我不断收到此错误:

Uncaught Error: Call to a member function query() on null

我对这种方法很陌生,但为什么我从查询中得到一个空值?

这是调用 classes 的代码:

<?php

    $data = new Data;
    echo $data->connect();
    
    $view = new View;
    echo $view->getData();
    
?>

这是查询 class 我怀疑有问题:

<?php 
    
    class View extends Data {
    
        public function getData() {
            $sql = 'SELECT * FROM equipment';
            $stm = $this->connect()->query($sql);
            while ($row = $stm->fetch()) {
                echo $row['manuName'] . '<br>';
            }
        }
    }
    
?>

这是连接class:

<?php 
class Data {
    private $dbHost = DB_HOST;
    private $dbUser = DB_USER;
    private $dbPass = DB_PASS;
    private $dbName = DB_NAME;

    private $dbHandler;
    private $error;

    public function connect() {
        $con = 'mysql:host=' . $this->dbHost . ';dbname=' . $this->dbName;
        $options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );

        try {
            $this->dbHandler = new PDO($con, $this->dbUser, $this->dbPass, $options);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
            echo $this->error;
        }
    }
}

我没有看到我的错误。如果有人能看到为什么我无法提取数据。

谢谢

正如评论中u_mulder所说,

your connect method will not return anything.

我将其更新如下,并对您的 getData() 方法进行了一些更改:

    public function connect() {
        $con = 'mysql:host=' . $this->dbHost . ';dbname=' . $this->dbName;
        $options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );

        try {
            //$this->dbHandler = new PDO($con, $this->dbUser, $this->dbPass, $options);

            return  new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
            echo $this->error;
        }
    }
}


class View extends Data {


    public function getData($table){
        try {
            $sql="SELECT * FROM $table";
            $q = $this->connect()->query($sql) or die("failed!");

            while($r = $q->fetch(PDO::FETCH_ASSOC)){  $data[]=$r;  }
            return $data;

        }
        catch(PDOException $e)
        {
            echo 'Query failed'.$e->getMessage();
        }

    }


}



$view = new View;
$result = $view->getData('equipment');
print_r($result);

尽管我更喜欢删除连接方法并在您的数据中添加构造函数 class,如下所示:

    public $this->conn;
    public function __construct(){

        $this->conn = new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass);

    }

然后如下更改我的 getData:

$q = $this->conn->query($sql) or die("failed!");

‌因为正如阿迪森在评论中所说:

you shouldn't really be connecting again every time you run a query..

再次感谢 Ali、ADyson 和 u_mulder,

如果我理解所有评论的意思,那么这段代码应该考虑到您的建议。如果您有时间,能否请您告诉我这是否是更好的方法。

数据Class:

<?php 
class Data {
    private $dbHost = DB_HOST;
    private $dbUser = DB_USER;
    private $dbPass = DB_PASS;
    private $dbName = DB_NAME;
    private $error;

    public $this->conn;
    public function __construct(){

        $this->conn = new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass);
    }

        try {
            return new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName,$this->dbUser,$this->dbPass);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
            echo $this->error;
        }
    }
}

查看Class:

<?php 

class View extends Data {


    public function getData($table){
        try {
            $sql="SELECT * FROM $table";
            $q = $this->conn->query($sql) or die("failed!");
           
            while($r = $q->fetch(PDO::FETCH_ASSOC)){  $data[]=$r;  }
            return $data;

        }
        catch(PDOException $e)
        {
            echo 'Query failed'.$e->getMessage();
        }

    }


}

输出:

<?php
$view = new View;
$result = $view->getData('equipment');
print_r($result);    
?>

这段代码仍然报错:

unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';'