1<?php 2/* vim: set expandtab tabstop=4 shiftwidth=4: */ 3// +----------------------------------------------------------------------+ 4// | PHP version 5 | 5// +----------------------------------------------------------------------+ 6// | Copyright (c) 1997-2004 The PHP Group | 7// +----------------------------------------------------------------------+ 8// | This source file is subject to version 3.0 of the PHP license, | 9// | that is bundled with this package in the file LICENSE, and is | 10// | available through the world-wide-web at the following url: | 11// | http://www.php.net/license/3_0.txt. | 12// | If you did not receive a copy of the PHP license and are unable to | 13// | obtain it through the world-wide-web, please send a note to | 14// | license@php.net so we can mail you a copy immediately. | 15// +----------------------------------------------------------------------+ 16// | Authors: Original Author <author@example.com> | 17// | Your Name <you@example.com> | 18// +----------------------------------------------------------------------+ 19// 20// $Id:$ 21 22class pdomysql { 23 public $dbtype = 'mysql'; 24 public $dbhost = '127.0.0.1'; 25 public $dbport = '3306'; 26 public $dbname = 'test'; 27 public $dbuser = 'root'; 28 public $dbpass = ''; 29 public $charset = 'utf-8'; 30 public $stmt = null; 31 public $DB = null; 32 public $connect = true; // 是否長连接 33 public $debug = true; 34 private $parms = array(); 35 private $sql = array( 36 "db" => "", 37 "from" => "", 38 "where" => "", 39 "order" => "", 40 "limit" => "" 41 ); 42 /** 43 * 构造函数 44 */ 45 public function __construct() { 46 47 $this->connect(); 48 $this->DB->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); 49 $this->DB->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); 50 $this->execute('SET NAMES ' . $this->charset); 51 } 52 /** 53 * 析构函数 54 */ 55 public function __destruct() { 56 $this->close(); 57 } 58 /** 59 * *******************基本方法开始******************** 60 */ 61 /** 62 * 作用:连結数据库 63 */ 64 public function connect() { 65 try { 66 $this->DB = new PDO($this->dbtype . ':host=' . $this->dbhost . ';port=' . $this->dbport . ';dbname=' . $this->dbname, $this->dbuser, $this->dbpass, array( 67 PDO::ATTR_PERSISTENT => $this->connect 68 )); 69 } 70 catch(PDOException $e) { 71 die("Connect Error Infomation:" . $e->getMessage()); 72 } 73 } 74 /** 75 * 关闭数据连接 76 */ 77 public function close() { 78 $this->DB = null; 79 } 80 /** 81 * 對字串進行转義 82 */ 83 public function quote($str) { 84 return $this->DB->quote($str); 85 } 86 /** 87 * 作用:获取数据表里的欄位 88 * 返回:表字段结构 89 * 类型:数组 90 */ 91 public function getFields($table) { 92 $this->stmt = $this->DB->query("DESCRIBE $table"); 93 $result = $this->stmt->fetchAll(PDO::FETCH_ASSOC); 94 $this->stmt = null; 95 return $result; 96 } 97 /** 98 * 作用:获得最后INSERT的主鍵ID 99 * 返回:最后INSERT的主鍵ID 100 * 类型:数字 101 */ 102 public function getLastId() { 103 return $this->DB->lastInsertId(); 104 } 105 /** 106 * 作用:執行INSERT\UPDATE\DELETE 107 * 返回:执行語句影响行数 108 * 类型:数字 109 */ 110 public function execute($sql) { 111 $this->getPDOError($sql); 112 return $this->DB->exec($sql); 113 } 114 /** 115 * 获取要操作的数据 116 * 返回:合併后的SQL語句 117 * 类型:字串 118 */ 119 private function getCode($table, $args) { 120 $code = ''; 121 if (is_array($args)) { 122 foreach ($args as $k => $v) { 123 if ($v == '') { 124 continue; 125 } 126 $code.= "`$k`='$v',"; 127 } 128 } 129 $code = substr($code, 0, -1); 130 return $code; 131 } 132 public function optimizeTable($table) { 133 $sql = "OPTIMIZE TABLE $table"; 134 $this->execute($sql); 135 } 136 /** 137 * 执行具体SQL操作 138 * 返回:运行結果 139 * 类型:数组 140 */ 141 private function _fetch($sql, $type) { 142 $result = array(); 143 $this->stmt = $this->DB->query($sql); 144 $this->getPDOError($sql); 145 $this->stmt->setFetchMode(PDO::FETCH_ASSOC); 146 switch ($type) { 147 case '0': 148 $result = $this->stmt->fetch(); 149 break; 150 151 case '1': 152 $result = $this->stmt->fetchAll(); 153 break; 154 155 case '2': 156 $result = $this->stmt->rowCount(); 157 break; 158 } 159 $this->stmt = null; 160 return $result; 161 } 162 /** 163 * *******************基本方法結束******************** 164 */ 165 /** 166 * *******************Sql操作方法开始******************** 167 */ 168 /** 169 * 作用:插入数据 170 * 返回:表內記录 171 * 类型:数组 172 * 參数:$db->insert('$table',array('title'=>'Zxsv')) 173 */ 174 public function add($table, $args) { 175 $sql = "INSERT INTO `$table` SET "; 176 $code = $this->getCode($table, $args); 177 $sql.= $code; 178 return $this->execute($sql); 179 } 180 /** 181 * 修改数据 182 * 返回:記录数 183 * 类型:数字 184 * 參数:$db->up($table,array('title'=>'Zxsv'),array('id'=>'1'),$where 185 * ='id=3'); 186 */ 187 public function up($table, $args, $where) { 188 $code = $this->getCode($table, $args); 189 $sql = "UPDATE `$table` SET "; 190 $sql.= $code; 191 $sql.= " Where $where"; 192 return $this->execute($sql); 193 } 194 /** 195 * 作用:刪除数据 196 * 返回:表內記录 197 * 类型:数组 198 * 參数:$db->del($table,$condition = null,$where ='id=3') 199 */ 200 public function del($table, $where) { 201 $sql = "DELETE FROM `$table` Where $where"; 202 return $this->execute($sql); 203 } 204 /** 205 * 作用:获取單行数据 206 * 返回:表內第一条記录 207 * 类型:数组 208 * 參数:$db->fetOne($table,$condition = null,$field = '*',$where ='') 209 */ 210 public function fetOne($table, $field = '*', $where = false) { 211 $sql = "SELECT {$field} FROM `{$table}`"; 212 $sql.= ($where) ? " WHERE $where" : ''; 213 return $this->_fetch($sql, $type = '0'); 214 } 215 /** 216 * 作用:获取所有数据 217 * 返回:表內記录 218 * 类型:二維数组 219 * 參数:$db->fetAll('$table',$condition = '',$field = '*',$orderby = '',$limit 220 * = '',$where='') 221 */ 222 public function fetAll($table, $field = '*', $orderby = false, $where = false) { 223 $sql = "SELECT {$field} FROM `{$table}`"; 224 $sql.= ($where) ? " WHERE $where" : ''; 225 $sql.= ($orderby) ? " ORDER BY $orderby" : ''; 226 return $this->_fetch($sql, $type = '1'); 227 } 228 /** 229 * 作用:获取單行数据 230 * 返回:表內第一条記录 231 * 类型:数组 232 * 參数:select * from table where id='1' 233 */ 234 public function getOne($sql) { 235 return $this->_fetch($sql, $type = '0'); 236 } 237 /** 238 * 作用:获取所有数据 239 * 返回:表內記录 240 * 类型:二維数组 241 * 參数:select * from table 242 */ 243 public function getAll($sql) { 244 return $this->_fetch($sql, $type = '1'); 245 } 246 /** 247 * 作用:获取首行首列数据 248 * 返回:首行首列欄位值 249 * 类型:值 250 * 參数:select `a` from table where id='1' 251 */ 252 public function scalar($sql, $fieldname) { 253 $row = $this->_fetch($sql, $type = '0'); 254 return $row[$fieldname]; 255 } 256 /** 257 * 获取記录总数 258 * 返回:記录数 259 * 类型:数字 260 * 參数:$db->fetRow('$table',$condition = '',$where =''); 261 */ 262 public function fetRowCount($table, $field = '*', $where = false) { 263 $sql = "SELECT COUNT({$field}) AS num FROM $table"; 264 $sql.= ($where) ? " WHERE $where" : ''; 265 return $this->_fetch($sql, $type = '0'); 266 } 267 /** 268 * 获取記录总数 269 * 返回:記录数 270 * 类型:数字 271 * 參数:select count(*) from table 272 */ 273 public function getRowCount($sql) { 274 return $this->_fetch($sql, $type = '2'); 275 } 276 //链式操作开始 277 public function from($tableName) { 278 $this->sql["from"] = "FROM " . $tableName; 279 return $this; 280 } 281 public function db($tableName) { 282 $this->sql["db"] = $tableName; 283 return $this; 284 } 285 public function where($_where = '1=1') { 286 $this->sql["where"] = "WHERE " . $_where; 287 return $this; 288 } 289 public function order($_order = 'id DESC') { 290 $this->sql["order"] = "ORDER BY " . $_order; 291 return $this; 292 } 293 public function limit($_limitstart="30",$_limitend = '0') { 294 if ($_limitend=="0"){ 295 $this->sql["limit"] = "LIMIT 0," . $_limitstart; 296 }else{ 297 $this->sql["limit"] = "LIMIT $_limitstart," . $_limitend; 298 } 299 300 return $this; 301 } 302 /** 303 * 获取所有記录 304 * 返回:所有記录 305 * 类型:array 306 * 參数: $pdomysql->from('dbname')->limit(30)->where('1=1')->select(); 307 */ 308 public function select($_select = '*') { 309 //return "SELECT " . $_select . " " . (implode(" ", $this->sql)); 310 $sql="SELECT " . $_select . " " . (implode(" ", $this->sql)); 311 return $this->_fetch($sql, $type = '1'); 312 } 313 /** 314 * 获取一条記录 315 * 返回:一条記录 316 * 类型:array 317 * 參数: $pdomysql->from('dbname')->limit(30)->where('1=1')->find(); 318 */ 319 public function find($_find = '*'){ 320 $this->sql['limit']='limit 1'; 321 $sql="SELECT " . $_find . " " . (implode(" ", $this->sql)); 322 return $this->_fetch($sql, $type = '0'); 323 } 324 /** 325 * 插入一条数据 326 * 返回:执行结果 327 * 类型:bool 328 * 參数: $pdomysql->db('dbname')->insert(array('col1'="sadsd")); 329 */ 330 public function insert($_insert=array()){ 331 $table=$this->sql['db']; 332 $sql = "INSERT INTO `$table` SET "; 333 $code = $this->getCode($table, $args); 334 $sql.= $code; 335 return $this->execute($sql); 336 } 337 /** 338 * 删除 339 * 返回:删除结果 340 * 类型:bool 341 * 參数: $pdomysql->from('dbname')->where('1=1')->delete(); 342 */ 343 public function delete(){ 344 $sql="DELETE " . (implode(" ", $this->sql)); 345 return $this->execute($sql); 346 } 347 public function update(){ 348 349 } 350 //链式操作end 351 /** 352 * *******************Sql操作方法結束******************** 353 */ 354 /** 355 * *******************错误处理开始******************** 356 */ 357 /** 358 * 設置是否为调试模式 359 */ 360 public function setDebugMode($mode = true) { 361 return ($mode == true) ? $this->debug = true : $this->debug = false; 362 } 363 /** 364 * 捕获PDO错误信息 365 * 返回:出错信息 366 * 类型:字串 367 */ 368 private function getPDOError($sql) { 369 $this->debug ? $this->errorfile($sql) : ''; 370 if ($this->DB->errorCode() != '00000') { 371 $info = ($this->stmt) ? $this->stmt->errorInfo() : $this->DB->errorInfo(); 372 echo ($this->sqlError('mySQL Query Error', $info[2], $sql)); 373 exit(); 374 } 375 } 376 private function getSTMTError($sql) { 377 $this->debug ? $this->errorfile($sql) : ''; 378 if ($this->stmt->errorCode() != '00000') { 379 $info = ($this->stmt) ? $this->stmt->errorInfo() : $this->DB->errorInfo(); 380 echo ($this->sqlError('mySQL Query Error', $info[2], $sql)); 381 exit(); 382 } 383 } 384 /** 385 * 寫入错误日志 386 */ 387 private function errorfile($sql) { 388 echo $sql . '<br />'; 389 $errorfile = __DIR__ . '/dberrorlog.php'; 390 $sql = str_replace(array( 391 "\n", 392 "\r", 393 "\t", 394 " ", 395 " ", 396 " " 397 ) , array( 398 " ", 399 " ", 400 " ", 401 " ", 402 " ", 403 " " 404 ) , $sql); 405 if (!file_exists($errorfile)) { 406 $fp = file_put_contents($errorfile, "<?PHP exit('Access Denied'); ?>\n" . $sql); 407 } else { 408 $fp = file_put_contents($errorfile, "\n" . $sql, FILE_APPEND); 409 } 410 } 411 /** 412 * 作用:运行错误信息 413 * 返回:运行错误信息和SQL語句 414 * 类型:字符 415 */ 416 private function sqlError($message = '', $info = '', $sql = '') { 417 $html = ''; 418 if ($message) { 419 $html.= $message; 420 } 421 if ($info) { 422 $html.= 'SQLID: ' . $info; 423 } 424 if ($sql) { 425 $html.= 'ErrorSQL: ' . $sql; 426 } 427 throw new Exception($html); 428 } 429 /** 430 * *******************错误处理結束******************** 431 */ 432}
PHP PDO_MYSQL 链式操作 非链式操作类
Wesley13
2021-10-11
1024 0 0
点赞
收藏
评论区
加载中...