PHP致命错误:调用成员函数prepare() classdbconnection{ 受保护的$连接; 公共函数构造(){ $this->dbConnect(); } 公共函数dbConnect(){ 试一试{ //如果尚未建立连接,请尝试连接到数据库{ //将配置加载为数组。使用配置文件的实际位置 $config=parse_ini_文件(“config.ini”); $dsn='mysql:host='.$config['host'.';dbname='.$config['dbname'.];charset=utf8;'; $this->connection=newpdo($dsn,$config['username'],$config['password']); //echo“已成功连接到数据库!”; 返回$this->connection; }捕获(例外$e){ echo“连接到数据库时出错!”; $this->connection=NULL; } } 公共职能(){ $this->connection=NULL; //echo“数据库连接已成功断开!”; } } 类DBTransaction扩展了DBconnection{ 公共函数构造(){ 父项::_构造(); } //获取所有数据 公共函数全选($query){ 试一试{ $result=$this->connection->query($query); $result->execute(); 返回$result; }捕获(PDO$e){ 抛出新异常($e->getMessage()); } } //获取特定数据 公共函数select_specific($query,$params=[])){ 试一试{ $result=$this->connection->prepare($query); $result->execute($params); 返回$result; }捕获(PDO$e){ 抛出新异常($e->getMessage()); } } //执行插入、更新、删除 公共函数execute_query($query,$params=[])){ 试一试{ $stmt=$this->connection->prepare($query); $stmt->execute($params); 返回$stmt; }捕获(PDO$e){ 抛出新异常($e->getMessage()); } } }

PHP致命错误:调用成员函数prepare() classdbconnection{ 受保护的$连接; 公共函数构造(){ $this->dbConnect(); } 公共函数dbConnect(){ 试一试{ //如果尚未建立连接,请尝试连接到数据库{ //将配置加载为数组。使用配置文件的实际位置 $config=parse_ini_文件(“config.ini”); $dsn='mysql:host='.$config['host'.';dbname='.$config['dbname'.];charset=utf8;'; $this->connection=newpdo($dsn,$config['username'],$config['password']); //echo“已成功连接到数据库!”; 返回$this->connection; }捕获(例外$e){ echo“连接到数据库时出错!”; $this->connection=NULL; } } 公共职能(){ $this->connection=NULL; //echo“数据库连接已成功断开!”; } } 类DBTransaction扩展了DBconnection{ 公共函数构造(){ 父项::_构造(); } //获取所有数据 公共函数全选($query){ 试一试{ $result=$this->connection->query($query); $result->execute(); 返回$result; }捕获(PDO$e){ 抛出新异常($e->getMessage()); } } //获取特定数据 公共函数select_specific($query,$params=[])){ 试一试{ $result=$this->connection->prepare($query); $result->execute($params); 返回$result; }捕获(PDO$e){ 抛出新异常($e->getMessage()); } } //执行插入、更新、删除 公共函数execute_query($query,$params=[])){ 试一试{ $stmt=$this->connection->prepare($query); $stmt->execute($params); 返回$stmt; }捕获(PDO$e){ 抛出新异常($e->getMessage()); } } },php,Php,问题是我遇到了以下错误: PHP致命错误:在中调用null上的成员函数prepare() /第58行的home/pipedu/public_html/msgbrd/class/DBTransaction.php 我在哪里犯了错误。当我在本地主机上运行时,它工作正常。在DBconnection::dbConnect()内的catch块中将类的connection属性设置为NULL使用$this->connection=NULL;。但是,在您的其他函数中,您不会检查它。通过不对该抛出错误,您可以使它

问题是我遇到了以下错误:

PHP致命错误:在中调用null上的成员函数prepare() /第58行的home/pipedu/public_html/msgbrd/class/DBTransaction.php


我在哪里犯了错误。当我在本地主机上运行时,它工作正常。

DBconnection::dbConnect()内的
catch
块中将类的
connection
属性设置为
NULL
使用
$this->connection=NULL;
。但是,在您的其他函数中,您不会检查它。通过不对该抛出错误,您可以使它成为类中的可接受状态

您的解决方案是:

  • 不要从类中的数据库连接捕获异常,而是在初始化它的实例时捕获它,并相应地处理它。这样
    DBconnection->connection=NULL
    将成为类中的非法状态,您可以在其他方法中忽略这种情况
  • 例如:

    class DBconnection {
    
        protected $connection;
    
        public function __construct(){
            $this->dbConnect();
        }
    
        public function dbConnect() {
            try {
    
                // Try and connect to the database, if a connection has not been established yet{
                // Load configuration as an array. Use the actual location of your configuration file
                $config = parse_ini_file("config.ini"); 
                $dsn = 'mysql:host='. $config['host'] .';dbname=' . $config['dbname'] . ';charset=utf8;';
                $this->connection = new PDO($dsn, $config['username'], $config['password']);
    
                //echo 'Connection to database has been successfull!';
                return $this->connection;
    
            } catch(Exception $e) {
                echo 'There was an error while connecting to the database!';
                $this->connection = NULL;
            }
        }
    
        public function disConnect(){
            $this->connection = NULL;
            //echo '<br>Database connection has been disconnected successfully!';
        }
    }
    
    class DBTransaction extends DBconnection {
    
        public function __construct(){
            parent::__construct();
        }
    
        // FETCH ALL DATA
        public function select_all($query) {
          try {
                $result = $this->connection->query($query);
                    $result->execute();
                        return $result;
    
            } catch (PDOException $e) {
                throw new Exception($e->getMessage());  
            }
      }
    
      // FETCH SPECIFIC DATA
      public function select_specific($query, $params = []) {
        try {
                $result = $this->connection->prepare($query);
                  $result->execute($params);
                    return $result;
    
            } catch (PDOException $e) {
                throw new Exception($e->getMessage());  
            }
      }
    
      // EXECUTE INSERT, UPDATE, DELETE
        public function execute_query($query, $params = []) {
            try {
                $stmt = $this->connection->prepare($query);
                    $stmt->execute($params);
                        return $stmt;   
    
            } catch (PDOException $e) {
                throw new Exception($e->getMessage());  
            }
        }
    }
    
    try {
        $con = new DBTransaction();
        $result = $con->select_all('SELECT * FROM table');
    } catch (Exception $e) {
        $result = NULL;
    }
    
  • 处理状态,即在其他方法中,
    DBconnection->connection
    可能是
    NULL
    ,并为该情况返回并批准值:
  • 例如:

    class DBconnection {
    
        protected $connection;
    
        public function __construct(){
            $this->dbConnect();
        }
    
        public function dbConnect() {
            try {
    
                // Try and connect to the database, if a connection has not been established yet{
                // Load configuration as an array. Use the actual location of your configuration file
                $config = parse_ini_file("config.ini"); 
                $dsn = 'mysql:host='. $config['host'] .';dbname=' . $config['dbname'] . ';charset=utf8;';
                $this->connection = new PDO($dsn, $config['username'], $config['password']);
    
                //echo 'Connection to database has been successfull!';
                return $this->connection;
    
            } catch(Exception $e) {
                echo 'There was an error while connecting to the database!';
                $this->connection = NULL;
            }
        }
    
        public function disConnect(){
            $this->connection = NULL;
            //echo '<br>Database connection has been disconnected successfully!';
        }
    }
    
    class DBTransaction extends DBconnection {
    
        public function __construct(){
            parent::__construct();
        }
    
        // FETCH ALL DATA
        public function select_all($query) {
          try {
                $result = $this->connection->query($query);
                    $result->execute();
                        return $result;
    
            } catch (PDOException $e) {
                throw new Exception($e->getMessage());  
            }
      }
    
      // FETCH SPECIFIC DATA
      public function select_specific($query, $params = []) {
        try {
                $result = $this->connection->prepare($query);
                  $result->execute($params);
                    return $result;
    
            } catch (PDOException $e) {
                throw new Exception($e->getMessage());  
            }
      }
    
      // EXECUTE INSERT, UPDATE, DELETE
        public function execute_query($query, $params = []) {
            try {
                $stmt = $this->connection->prepare($query);
                    $stmt->execute($params);
                        return $stmt;   
    
            } catch (PDOException $e) {
                throw new Exception($e->getMessage());  
            }
        }
    }
    
    try {
        $con = new DBTransaction();
        $result = $con->select_all('SELECT * FROM table');
    } catch (Exception $e) {
        $result = NULL;
    }
    

    第五十八行您的数据库连接刚刚失败,因此,
    连接
    空的
    。请不要为现有的数据库api编写另一个包装。在
    数据库连接
    中,
    $this->connection=null;
    是一个错误。如果您不能正确处理连接失败的事件,则根本不处理异常,并且让它传播,这样你就可以看到它失败了。@mickmackusa如果它确实是一个凭证问题,那么我可以说它属于“打字错误”的范畴喜欢错误,所以可能会偏离主题。但是,这里没有足够的信息让我们确定这与凭据有关,这也是吞咽异常的副作用(这也使它偏离主题,因为它不清楚)。谢谢大家的意见,我回家后会检查这一点。