php - Call to a member function query() on null with PDO -


i checked of answers available on stackoverflow,they similar not answer exactly. please don't take post duplicate.

these codes when i'm executing say's

fatal error: call member function query() on null in c:\wamp64\www\ourcms\index.php on line 12

here snippet :

<?php  class db {     private $dbhost;     private $dbname;     private $dbuser;     private $dbpass;      protected $con;      function set_db($host, $db, $user, $pass)     {         $this->dbhost = $host;         $this->dbname = $db;         $this->dbuser = $user;         $this->dbpass = $pass;      }      function connect()     {         $info = 'mysql:host='.$this->dbhost.';dbname='.$this->dbname;         try         {             $this->con = new pdo($info, $this->dbuser, $this->dbpass);         }         catch(pdoexception $e)         {             print "error founds: ".$e->getmessage().php_eol;             die();         }     } }  // here place i'm trying use if (isset($_post['submit'])) {     include('include/database.php');      $database = new db;     $database->set_db("localhost", "ourcms", "root", "");     $conn = $database->connect();      $name = $_post['nm'];     $query = "insert testingpdo (name) values ('$name')";     $data = $conn->query($query);     $result = $data->fetchall(pdo::fetch_assoc);      print_r($result); } 

you don't return nothing db::connect ($conn = $database->connect();). add return $this->con; @ end of function.

function connect() {     $info = 'mysql:host='.$this->dbhost.';dbname='.$this->dbname;     try     {         $this->con = new pdo($info, $this->dbuser, $this->dbpass);     }     catch(pdoexception $e)     {         print "error founds: ".$e->getmessage().php_eol;         die();     }     return $this->con; } 

Comments