PHP&;MySQLi使用prepared语句


PHP & MySQLi using prepared statement

我想使用单例设计模式来使用已建立的连接。我需要帮助如何实现这一点,因为我无法查询一些数据

db.config.hp:

define ('HOST','localhost');
define ('USER','garodamas_mon');
define ('PASSWORD','r0d4m45');
define ('DATABASE','garodamas_cashrcv');
class Database{
    private $DBH;
    private static $singleton;
    protected function __construct(){
        $this->DBH=new mysqli(HOST,USER,PASSWORD,DATABASE);
    }
    public static function instance(){
        if (!(self::$singleton instanceof self)) {
            self::$singleton = new self();
        }
        return self::$singleton;
    }
    public static function get(){
        return self::instance()->DBH;
    }
    private function __wake(){}
    private function __clone(){}
} 

和loginmodel.php:

require_once '../db.config.php';
$db = DATABASE::getInstance();
 $mysqli = $db->getConnection(); 
    $sql_query = "SELECT user_name FROM mst_user";
    $result = $mysqli->query($sql_query);

错误消息是

致命错误:在第5行调用C:''examplep''htdocs''SOBCASHIER''resources''templates''loginmodel.php中未定义的方法Database::getInstance()

您没有getInstance方法-您定义的方法称为instance:

$db = DATABASE::instance();