1. Класс
class DB {
private $_connection;
private static $_instance; //The single instance
private $_host = "localhost";
private $_username = "user";
private $_password = "pass";
private $_database = "db";
/*
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
// Constructor
private function __construct() {
$this->_instance = new mysqli($this->_host, $this->_username,
$this->_password, $this->_database);
// Error handling
if(mysqli_connect_error()) {
trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
E_USER_ERROR);
}
}
}
2.Код:
include 'DB.php';
$db = DB::getInstance();
$sql = "SELECT * FROM `table` LIMIT 1 ";
$m = mysqli_query($db, $sql);
var_dump( mysqli_fetch_assoc( $m ) );
Выводит всегда NULL. Хотя в $db записывается подключение. Отчего так?