Fatal error: Call to a member function query() PHP CLASS

前端 未结 2 476
花落未央
花落未央 2021-01-15 19:20

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

Fatal error: Call to a member function query() on null in C:

相关标签:
2条回答
  • 2021-01-15 19:48

    Your method:

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

    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. Connect to the DB on the instantiation of the DB class

    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. Lazy connection, i.e connect only on first executed query

    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>";
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-15 20:04

    You have to use $this to access properties of the own class.

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

    It would be better if you used a constructor to initiate the connection, currently you are opening a new connection to the database every time you use the query method.

    edit: Your class could look like this

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

    I see no need for the properties to be static, so I changed them too.

    The db_query method is in my opinion too unflexible, as it directly outputs your result. I would use fetch_all to return the whole resultset as an array. That way you can freely choose how you want to handle your results.

    0 讨论(0)
提交回复
热议问题