本文實例講述了php封裝的mysqli類。分享給大家供大家參考,不足之處歡迎指出。
1 <?php 2 3 class Mysql 4 { 5 6 private $host; 7 8 private $user; 9 10 private $password; 11 12 private $charset; 13 14 private $database; 15 16 /** 17 * 新建數據庫連接對象,測試數據庫連接 18 * 19 * @param string $host 20 * @param string $user 21 * @param string $password 22 * @param string $charset 23 * @param string $database 24 */ 25 function __construct($host, $user, $password, $charset, $database) 26 { 27 $link = mysqli_connect($host, $user, $password) or die('數據庫連接失敗<br />ERROR ' . mysqli_connect_errno() . ':' . mysqli_connect_error()); 28 $char = mysqli_set_charset($link, $charset) or die('charset設置錯誤,請輸入正確的字符集名稱<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link)); 29 $db = mysqli_select_db($link, $database) or die('未找到數據庫,請輸入正確的數據庫名稱<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link)); 30 $this->host = $host; 31 $this->user = $user; 32 $this->password = $password; 33 $this->charset = $charset; 34 $this->database = $database; 35 mysqli_close($link); 36 } 37 38 /** 39 * 連接數據庫 40 * 41 * @param string $host 42 * @param string $user 43 * @param string $password 44 * @param string $charset 45 * @param string $database 46 * @return object 連接標識符 47 */ 48 private function connect($host, $user, $password, $charset, $database) 49 { 50 $link = mysqli_connect($this->host, $this->user, $this->password); 51 mysqli_set_charset($link, $this->charset); 52 mysqli_select_db($link, $this->database); 53 return $link; 54 } 55 56 /** 57 * 增加數據 58 * 59 * @param array $data 60 * @param string $table 61 * @return boolean 62 */ 63 public function insert($data, $table) 64 { 65 $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database); 66 $keys = join(',', array_keys($data)); 67 $vals = "'" . join("','", array_values($data)) . "'"; 68 $query = "INSERT INTO {$table}({$keys}) VALUES({$vals})"; 69 $result = mysqli_query($link, $query) or die('插入數據出錯,請檢查!<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link)); 70 if ($result) { 71 $id = mysqli_insert_id($link); 72 } else { 73 $id = false; 74 } 75 mysqli_close($link); 76 return $id; 77 } 78 79 /** 80 * 刪除數據 81 * 82 * @param string $table 83 * @param string $where 84 * @return boolean 85 */ 86 public function delete($table, $where = null) 87 { 88 $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database); 89 $where = $where ? ' WHERE ' . $where : ''; 90 $query = "DELETE FROM {$table}{$where}"; 91 $result = mysqli_query($link, $query) or die('刪除數據出錯,請檢查!<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link)); 92 if ($result) { 93 $row = mysqli_affected_rows($link); 94 } else { 95 $row = false; 96 } 97 mysqli_close($link); 98 return $row; 99 } 100 101 /** 102 * 修改數據 103 * 104 * @param array $data 105 * @param string $table 106 * @param string $where 107 * @return boolean 108 */ 109 public function update($data, $table, $where = null) 110 { 111 $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database); 112 $set = ''; 113 foreach ($data as $key => $val) { 114 $set .= "{$key}='{$val}',"; 115 } 116 $set = trim($set, ','); 117 $where = $where ? ' WHERE ' . $where : ''; 118 $query = "UPDATE {$table} SET {$set}{$where}"; 119 $result = mysqli_query($link, $query) or die('數據修改錯誤,請檢查!<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link)); 120 if ($result) { 121 $row = mysqli_affected_rows($link); 122 } else { 123 $row = false; 124 } 125 mysqli_close($link); 126 return $row; 127 } 128 129 /** 130 * 查詢指定記錄 131 * 132 * @param string $query 133 * @param string $result_type 134 * @return array|boolean 135 */ 136 public function select_one($query, $result_type = MYSQLI_ASSOC) 137 { 138 $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database); 139 $result = mysqli_query($link, $query) or die('查詢語句錯誤,請檢查!<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link)); 140 if ($result && mysqli_num_rows($result) > 0) { 141 $row = mysqli_fetch_array($result, $result_type); 142 } else { 143 $row = false; 144 } 145 mysqli_free_result($result); 146 mysqli_close($link); 147 return $row; 148 } 149 150 /** 151 * 查詢所有記錄 152 * 153 * @param string $query 154 * @param string $result_type 155 * @return array|boolean 156 */ 157 public function select_all($query, $result_type = MYSQLI_ASSOC) 158 { 159 $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database); 160 // $query = "SELECT * FROM {$table}"; 161 $result = mysqli_query($link, $query) or die('查詢語句錯誤,請檢查!<br />ERROR ' . mysqli_errno($link) . ':' . mysqli_error($link)); 162 if ($result && mysqli_num_rows($result) > 0) { 163 while ($row = mysqli_fetch_array($result, $result_type)) { 164 $rows[] = $row; 165 } 166 } else { 167 $rows = false; 168 } 169 mysqli_free_result($result); 170 mysqli_close($link); 171 return $rows; 172 } 173 174 /** 175 * 得到表中記錄數 176 * 177 * @param string $table 178 * @return number|boolean 179 */ 180 public function get_total_rows($table) 181 { 182 $link = $this->connect($this->host, $this->user, $this->password, $this->charset, $this->database); 183 $query = "SELECT COUNT(*) AS totalRows FROM {$table}"; 184 $result = mysqli_query($link, $query); 185 if ($result && mysqli_num_rows($result) == 1) { 186 $row = mysqli_fetch_assoc($result); 187 } else { 188 $row['totalRows'] = false; 189 } 190 mysqli_close($link); 191 return $row['totalRows']; 192 } 193 }