gosulove gosulove - 3 months ago 8
MySQL Question

php constructor is not working correctly with error query() on null

As you can see from item.php, I have 2 functions to display some items

display_item_1() and display_item_2()

When I run the program by calling

$item = new item();
$item->display_item_1(1);
$item->display_item_2(1);


It only displays function->display_item_1()

but for display_item_2() receives Fatal error: Call to a member function query() on null

In other words, both function display_item_1() and display_item_2() are same coding but just display the 1st one.
So that means the constructor in item.php is not correct?

OR which part did i do wrong?

db.php

class db{

protected $db_host;
protected $db_name;
protected $db_user_name;
protected $db_pass;

public function __construct() {
$this->db_host="localhost";
$this->db_name="bs";
$this->db_user_name="root";
$this->db_pass="";
}

public function conn(){

try {
$conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user_name="root", $this->db_pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}


item.php

require "db.php";

class item {

public $user_uid;
protected $item_name;
public $conn;

public function __construct(){
$db = new db();
$this->conn= $db->conn();

}

public function display_item_1($uid){

$this->user_uid=$uid;

try{
$sql="SELECT * FROM item where uid='$this->user_uid'";
$statement=$this->conn->query($sql);
}
catch(PDOException $e){
...
}

}

public function display_item_2($uid){

$this->user_uid=$uid;

try{
$sql="SELECT * FROM item where uid='$this->user_uid'";
$statement=$this->conn->query($sql);
}

}
catch(PDOException $e){
...
}

}
}

Answer

It has also been mentioned in your other question and I'll mention it again in this one: rewrite your code and let the DB class handle the querying. Right now your DB class doesn't make sense in terms of object-oriented design, it's not well written and should be rewritten from scratch. Here's just an idea how it could look like:

db.php

// Use uppercase for class names and stick to possibly unreserved names
class DatabaseHandler
{

    // moved from constructor and set to private, you are surely not using inheritance
    private $db_host="localhost";
    private $db_name="bs";
    private $db_user_name="root";
    private $db_pass="";
    private $connection= FALSE; // added, holds db connection

    /* obsolete in your case 
    public function __construct() { }
    */

    public function connect() 
    {

        try {   
            $conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user_name="root", $this->db_pass);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->connection= $conn;
            return TRUE;
        }
        catch(PDOException $e)
        {
            echo $sql . "<br>" . $e->getMessage();
            return FALSE;
        }
    }

    /**
    * return records from your database; checks skipped in this example
    */
    public function query($query) 
    {
        // only query if a database connection is available, else return false
        if (!$this->connection) return FALSE;
        try {
            $statement=$this->connection->query($query);
            return $statement->fetchAll();
        }
        catch(PDOException $e){
            // logging or whatever ...
            return FALSE;           
        }
    }
}

item.php

class Item {

    public $user_uid;
    private $item_name;
    private $conn; // why make it public?

    public function __construct(){
        $db = new DatabaseHandler();
        if (!$db->connect()) die("Database not available"); // update these lines
        $this->conn= $db; 
    }

    public function display_item_1($uid){
        $this->user_uid=$uid;

        $arrValues= $this->conn->query("SELECT * FROM item where uid='$this->user_uid'");
        print_r($arrValues);
    }

    public function display_item_2($uid){
        $this->user_uid=$uid;

        $arrValues= $this->conn->query("SELECT * FROM item where uid='$this->user_uid'");
        print_r($arrValues);
    }
}

Run code:

$item = new Item();
$item->display_item_1(1);
$item->display_item_2(1);

And please go through some tutorials, especially classes and OOP (object-oriented programming).

Comments