致命错误:调用成员函数query()PHP CLASS [英] Fatal error: Call to a member function query() PHP CLASS

查看:84
本文介绍了致命错误:调用成员函数query()PHP CLASS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写此类来连接和查询我的数据库,但出现此错误:

I'm trying to write this class to connect and query my database, but I got this error:

致命错误:在null中调用成员函数query() 第24行的C:\ xxxxxx \ xxxx \ xxxxx \ xxxxxxx \ pages \ config \ class.php

Fatal error: Call to a member function query() on null in C:\xxxxxx\xxxx\xxxxx\xxxxxxx\pages\config\class.php on line 24

Php代码:

<?php

class Db{

    private static $db_host = "localhost";
    private static $db_user = "root";
    private static $db_pass = "";
    private static $db_name = "sivi";

    public $connection;

        public function db_connect() {    

            $connection = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name) or die("Error " . mysqli_error($connection)); 
            echo "Conexión realizada". "<br>";

    }  

    public function db_query($query){

          $connection = $this->db_connect();
          var_dump($query);
          $result = $connection->query($query);
          while($row = mysqli_fetch_array($result)) { 
          echo $row["COD_PRE"] . "<br>";
          }

    } 

}

$con = new Db();
$con->db_query('SELECT `COD_PRE`, `CODE` FROM `test` WHERE `CODE` = 457 AND CONFIN = 1');

?>

推荐答案

您的方法:

public function db_connect() {    

            $connection = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name) or die("Error " . mysqli_error($connection)); 
            echo "Conexión realizada". "<br>";

    }  

不会从方法中调回$connection,因此rest方法调用失败:

does not retun the $connection back from the method so the rest method calls fail do this:

public function db_connect() {    

            $connection = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name) or die("Error " . mysqli_error($connection)); 
            echo "Conexión realizada". "<br>";
           return $connection; // return the $connection object
    }  

如前所述,您的代码效率不高,因为对于每个执行的查询,代码都(重新)连接到数据库.这是不必要的昂贵/低效的.

As mentioned, your code is not efficient since for every query performed the code (re-)connects to the DB. THis is unnecessarily expensive/inefficient.

有很多方法可以解决这个问题.

There are many approaches to solve this.

  1. 在数据库类的实例上连接到数据库

例如

class Db{

    private static $db_host = "localhost";
    private static $db_user = "root";
    private static $db_pass = "";
    private static $db_name = "sivi";

    public $connection;

    public function __construct()
   {
      $this->connection = $this->db_connect();
   }  

    public function db_connect() {    

            $connection = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name) or die("Error " . mysqli_error($connection)); 
            echo "Conexión realizada". "<br>";
            return $connection;
    }  
}

  1. 惰性连接,即仅在首次执行查询时进行连接

例如

class Db{

    private static $db_host = "localhost";
    private static $db_user = "root";
    private static $db_pass = "";
    private static $db_name = "sivi";
    public $connection = null;
    public function __construct()
    {
    }  
    public function db_connect() {    
        $connection = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name) or die("Error " . mysqli_error($connection)); 
        echo "Conexión realizada". "<br>";
        return $connection;
    }  
    public function db_query($query){
        if ( null ==== $this->connection ) $this->connection = $this->db_connect();
        var_dump($query);
        $result = $this->connection->query($query);
        while($row = mysqli_fetch_array($result)) { 
            echo $row["COD_PRE"] . "<br>";
        }
    }
}

这篇关于致命错误:调用成员函数query()PHP CLASS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆