I have the following code that is able to create a class that has a single static instance of the database object, and two static functions for rows and columns.
<?php class Database{
private static $instance;
private function __construct() {}
private function __clone(){}
public static function call(){
if(!isset(self::$instance)){
self::$instance = new MySQLi("localhost", "foo", "bar", "fizz");
if(self::$instance->connect_error){
throw new Exception('Error MySQL: ' . self::$instance->connect_error);
}
}
return self::$instance;
}
public static function getRow($id, $db){
$query = "SELECT * FROM ".$db." WHERE id=".$id;
$result = self::call()->query($query);
return $result->fetch_assoc();
}
} ?>
However, when I call getRow from another class, like this
$data = Database::getRow($id, "posts");
I get the following error:
Fatal error: Call to a member function fetch_assoc() on a non-object in database.php on line 27
And I check it again and again and everything seems to be in order, maybe I have a error in call() ? help!!.