mysqli_query資料庫有資料,查不出來
阿新 • • 發佈:2018-12-11
MySQLDB.class.php
<?php /** * 資料庫操作工具類 */ class MySQLDB { // 定義相關屬性 private $host;// 主機地址 private $port;// 埠號 private $user;// 使用者名稱 private $pass;// 使用者密碼 private $charset;// 字符集 private $dbname;// 資料庫名 private $link;// 執行的時候需要的屬性,儲存連線資源 // 增加私有屬性,使用者儲存單例物件 private static$instance; /** * 構造方法 * @param array $config */ public function __construct($config) { // 初始化相關屬性 $this->initParams($config); // 連線資料庫 $this->my_connect(); } /** * 初始化相關屬性 * @param array $config */ private function initParams($config) { $this->host = isset($config['host']) ? $config['host'] : 'localhost'; $this->port = isset($config['port']) ? $config['port'] : '3306'; $this->user = isset($config['user']) ? $config['user'] : 'root'; $this->pass = isset($config['pass']) ? $config['pass'] : ''; $this->charset = isset($config['charset']) ? $config['charset'] : 'utf8'; $this->dbname = isset($config['dbname']) ? $config['dbname'] : 'test'; } /** * 連線資料庫 */ private function my_connect() { if($link = mysqli_connect($this->host, $this->user, $this->pass, $this->dbname)) { // 連線成功,應該將連結資源儲存到屬性中 $this->link = $link; }else { // 連線失敗 echo '資料庫連線失敗!<br />'; echo '錯誤程式碼:',mysql_errno(),'<br />'; echo '錯誤資訊:',mysql_error(),'<br />'; return false; } } }
test.php
<?php header('Content-type:text/html;Charset=utf-8'); // 載入資料庫工具類 include './MySQLDB.class.php'; //設定相關的屬性 $config = array( 'pass' => '123456', 'dbname' => 'dbo' ); //例項化一個物件 $db = new MySQLDB($config); $sql = "select * from `dbo`.optiobookigtools_user"; $result = mysqli_query($db,$sql); // 返回記錄數 $rowcount=mysqli_num_rows($result); printf("總共返回 %d 行資料。",$rowcount); // 釋放結果集 var_dump(mysqli_free_result($result));