致命错误:调用成员函数query()PHP CLASS

时间:2015-06-03 19:06:49

标签: php mysql oop

我正在尝试编写此类来连接和查询我的数据库,但是我收到了这个错误:

  

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

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');

?>

2 个答案:

答案 0 :(得分:2)

你的方法:

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,因此其余方法调用失败,请执行以下操作:

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
    }  

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

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

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

    e.g

    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. 延迟连接,即仅在第一次执行的查询时连接
    2. e.g

      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>";
              }
          }
      }
      

答案 1 :(得分:2)

您必须使用$this来访问自己类的属性。

$this->connection = ....

$result = $this->connection->...

如果使用构造函数来启动连接会更好,目前每次使用查询方法时都会打开与数据库的新连接。

编辑:你的课程看起来像这样

<?php

class Db {

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

    public function __construct() {
        $this->connection = mysqli_connect($this->db_host, $this->db_user, $this->db_pass, $this->db_name) 
                or die("Error " . mysqli_error($this->connection));
    }

    public function db_query($query) {
        var_dump($query);
        $result = $this->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');
?>

我认为属性不需要是静态的,所以我也改变了它们。

db_query方法在我看来太不灵活,因为它直接输出你的结果。我会使用fetch_all将整个结果集作为数组返回。 这样您就可以自由选择处理结果的方式。