Jump to content

My Mysqli Class problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
squirll

squirll

    Newbie

  • Members
  • Pip
  • 3 posts
Hi guys...I trying to fix this problem i make my class for mysqli and it is...



class DB {


public function __construct($host, $user, $pass, $base){

  $mysqli = new mysqli($host, $user, $pass);

  

  if($mysqli->connect_error){

  throw new Exception ('Mysql Connect Error: ('.$mysqli->connect_errono.')'.$mysqli->connect_error);

    }else{

  $this->mysqli->select_db($base); 

  }

  return $this->mysqli;

}


public function execute($sql){

  $this->mysqli = $this->mysqli->query($sql);  #35 line

  return $this->mysqli;

}


public function __destruct(){

  @$this->mysqli->close();

}


} // End 



and i getting this error

Fatal error: Call to a member function query() on a non-object in /var/www/db.class.php on line 35
Where is problem?

P.s PHP version is 5.3.3

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
From what I can see, you are defining $mysqli only within __construct. You must define it out of the function scope so the class can access it.

Maybe something like this gives the idea:
<?php
class DB {
    //local class variable
    private $mysqli = null;
    
    public function __construct($host, $user, $pass, $base) {
        $this->mysqli = new mysqli($host, $user, $pass);

        if($this->mysqli->connect_error) {
            throw new Exception ('Mysql Connect Error: (' . $this->mysqli->connect_errono. ')' . $this->mysqli->connect_error);
        } else {
            $this->mysqli->select_db($base); 
        }
    }

    public function execute($sql) {
        //return resource object to user
        return $this->mysqli->query($sql);
    }

    public function __destruct() {
        $this->mysqli->close();
    }

} // End  
?>

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
squirll

squirll

    Newbie

  • Members
  • Pip
  • 3 posts
Ok, so that i get it but next problem....

I call class


$AID= '10';

$db = new DB ($user, $pass, $host, $base);

$sql = "SELECT * FROM articles WHERE AID='".$db->real_escape_string($AID)."' LIMIT 1";

$db->execute($sql);


I get

Quote

Fatal error: Call to undefined method DB::real_escape_string()
Than how to call real escape string? :S

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You did not define function real_escape_string() within your DB wrapper class. You can create a wrapper for that as well within your class:
public function real_escape_string($string) {
    //your own checks too, parent:: accesses mysqli class
    return parent::real_escape_string($string);
}
changing your first class line to:
class DB extends mysqli { 

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.