Multiple mysqli queries with php singleton

I have a very common mysqli class to provide database connections for the rest of the application:

class IabDB extends mysqli {

private static $instance = null;
// db connection config vars
private $user = "xxx-lit";
private $pass = "xxx";
private $dbName = "xxx";
private $dbHost = "xxx";


private function __construct() {
    parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
    if (mysqli_connect_error()) {
        exit('Connect Error (' . mysqli_connect_errno() . ') '
                . mysqli_connect_error());
    } 
    mysqli_set_charset($this, "utf8");
}


public static function getInstance() {
    if (!self::$instance instanceof self) {
        self::$instance = new self;
    }
    return self::$instance;
}

In my model, I extract data that works well:

$db = IabDB::getInstance();
    $query = $db->query("SELECT status, username, email, id FROM " . $table);
    $items =  $query->fetch_all();

EDIT But when I try to get another instance for another request, I get an error message: call the fetch_all () member function on null.

Trust me: I tried it back and forth. Both queries are valid, and both return a result if only one of them is used. Just for completeness, here is the second model with the model:

class Model {

    public static function getItems($table) {      
            $db = IabDB::getInstance();
            $query = $db->query("SELECT * FROM lit_veranstaltungen");
            $items = $query->fetch_all();
            $db->close();
            var_dump($items);

}}

So, it seems that the second connection is broken somehow !?

Bonus question: according to my information, it is obvious that now, apparently, now basically laughed at a singleton template, is it well suited for this purpose?

!

+4
3

, , :

$db = IabDB::getInstance();
$query = $db->query("SELECT name FROM users limit 1");
$items =  $query->fetch_all();
var_export($items);

$db = IabDB::getInstance();
$query = $db->query("SELECT name FROM users limit 1,1");
$items =  $query->fetch_all();
var_export($items);

array ( 0 => array ( 0 => 'John', ), )
array ( 0 => array ( 0 => 'Mike', ), )

, , , . .

private function __construct() {
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
    $this->set_charset("utf8");
}

, PHP

error_reporting(E_ALL);
ini_set('display_errors',1);

.

, . , . , ,

$db = IabDB::getInstance();
$query = $db->query("SELECT name FROM users WHERE 1=0");
$items =  $query->fetch_all();
var_export($items);

$db = IabDB::getInstance();
$query = $db->query("SELECT name FOrM users");
$items =  $query->fetch_all();
var_export($items);

:

()
: "mysqli_sql_exception" " SQL; , MySQL, " " 1"

, ,

public function query_params($query, $params, $types = NULL)
{
    $statement = $this->prepare($query);
    $types = $types ?: str_repeat('s', count($params));
    $statement->bind_param($types, ...$params);
    $statement->execute();
    return $statement->get_result();
}

, PDO:

$user = $db->query_params("SELECT * FROM users WHERE name = ?", ['Mike'])->fetch_assoc();

singleton, PHP- , . , , .

+2

singleton , mysqli ( PDO), mysqli ( PDO) . ! , :

Fatal error: Access level to DataBase::__construct() must be public (as in class mysqli)
+1

$query->close(); .

- . - " ", mysqli , , .

- :

class DBConnection {
    protected static $connections = array();

    public static function getConnection($connection_name = 'default') {
        if (!isset(static::$connections[$connection_name])) {
            static::$connections[$connection_name] = // setup connection 
            // ... error handling and stuff
        }

        return static::$connections[$connection_name];
    }
}

Btw: mysqli :

http://php.net/manual/de/mysqli.connect-error.php

http://php.net/manual/de/mysqli.connect-errno.php

http://php.net/mysqli_set_charset

0

All Articles