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


i'm trying write class connect , query database, got error:

fatal error: call member function query() on null in c:\xxxxxx\xxxx\xxxxx\xxxxxxx\pages\config\class.php on line 24

php code:

<?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` `test` `code` = 457 , confin = 1');  ?> 

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 $connection method rest method calls fail 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 $connection object     }   

as mentioned, code not efficient since every query performed code (re-)connects db. unnecessarily expensive/inefficient.

there many approaches solve this.

  1. connect db on instantiation of 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 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>";         }     } } 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -