【问题标题】:PHP & MySQLi using prepared statementPHP & MySQLi 使用准备好的语句
【发布时间】:2016-07-22 08:48:10
【问题描述】:

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

db.config.php:

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:\xampp\htdocs\SOBCASHIER\resources\templates\loginmodel.php 中未定义的方法 Database::getInstance()

【问题讨论】:

    标签: php mysql methods mysqli


    【解决方案1】:

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

    $db = DATABASE::instance();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 2016-10-27
      • 1970-01-01
      相关资源
      最近更新 更多