开发者

MySQLi as an static class

开发者 https://www.devze.com 2023-01-04 15:54 出处:网络
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.

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", "fo开发者_运维知识库o", "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() ?


That will happen if you have an error in your SQL syntax -- did you check it and make sure there were no errors?

I would have error checking, something like

if( self::$instance->error ) {
    // Handle error
}


You are passing in a variable called $db to getRow(), maybe that should be a table?


query() will return false if the SQL query fails. You can add the following to handle this error:

$result = self::call()->query($query)
if($result === false)
    throw new Exception(call()->error); // throw exception, use MySQLi error as message

return $result->fetch_assoc();


public static function getRow($id,$table)
{
    $query = "SELECT * FROM ".$table." WHERE id=".$id." LIMIT 1";
    $result = $this->instance->query($query);
    return $result->fetch_assoc();
}


this line

if(!isset(self::$instance)){  

will have sometimes strange behaviours. checking isset() against a null value is subject to failings. try this:

private static $instance = null; // it defaults to null but hey, rather be on the sure side
// foo
if (self::$instance === null) {
// mysqli creation foo


I have tried this code and it works, you must have an error in the query. What may cause this error is:

  1. Check what you pass to Database::getRow(). There may be some non-numerical characters in your id

  2. Check if column name 'id' exists, this sounds funny but it happened to me a million times, i was querying column 'id' instead of 'product_id' and so on

  3. Check if table $db exists

  4. Make sure you don't switch the function parameters by an accident. Database::getRow(24, 'people'); instead of Database::getRow('people', 24);

I can't think of anything else that may cause this error.

0

精彩评论

暂无评论...
验证码 换一张
取 消