一个小工程的准备工作 - Php MySql Class
我比较喜欢的一个。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | <?php /** * 数据库操作类 * * @author Moyo * @package defaultPackage */ class Mysql { // 默认配置 private $_config_default = array ( 'debug' => false, 'host' => 'localhost:3306' , 'username' => 'root' , 'password' => '' , 'database' => 'mysql' , 'prefix' => '' , 'charset' => 'utf-8' , 'cached' => 'file://{root}/query_cache/' ); public $CACHE_HASH_SALT = 'sql.cache.uuland.org' ; public $CLIENT_MULTI_RESULTS = 131072; // 配置信息 private $_config = array (); private $_debug = true; private $_host = '' ; private $_username = '' ; private $_password = '' ; private $_database = '' ; private $_prefix = '' ; private $_charset = '' ; private $_cached = '' ; private $_fc_path = '' ; private $_mc_server = '' ; // 运行时变量 private $_dbc_handle = null; private $_query_handle = null; public $sql = '' ; private $_cache_key = '' ; private $_result = array (); private $_need_cache = false; // 数据库操作 private $_operate = '' ; private $_column = '' ; private $_where = array (); private $_order = array (); private $_limit = '' ; private $_data = array (); private $_cache = '' ; // 调试记录 private $_trace = array (); // 获取实例 public function getInstance() { return new self(); } // 私有化构造函数,禁止外部实例化 private function __construct(){} // 卸载实例时,自动释放资源,关闭连接 public function __destruct() { // 释放资源 $this ->free(); // 关闭连接 $this ->close(); } // 载入配置 public function config( $config ) { $this ->trace( 'public::config::load' ); $this ->_config = $config ; // 执行初始化 $this ->init(); } // 初始化 private function init() { $this ->trace( 'private::config::init_default' ); // 配置分析 foreach ( $this ->_config as $key => $val ) { $mkey = '_' . $key ; $this -> $mkey = isset( $this ->_config[ $key ]) ? $this ->_config[ $key ] : $this ->_config_default[ $key ]; } // 清理非debug模式下,之前记录的调试信息 if (! $this ->_debug) unset( $this ->_trace); // 缓存配置 $this ->trace( 'private::config::init_cache' ); $cache_conf = explode ( '://' , $this ->_cached); $this ->_cached = $cache_conf [0]; if ( $this ->_cached == 'file' ) { $this ->_fc_path = str_replace ( '{current}' , dirname( __FILE__ ), str_replace ( '{root}' , $_SERVER [ 'DOCUMENT_ROOT' ], $cache_conf [1])); // 检测目录 if (! is_dir ( $this ->_fc_path)) { mkdir ( $this ->_fc_path); } } elseif ( $this ->_cached == 'memcache' ) { $this ->_mc_server = $cache_conf [1]; } unset( $this ->_config); } // 连接至数据库 private function connect() { $this ->trace( 'public::server::connect' ); // 连接服务器 $this ->_dbc_handle = mysql_connect( $this ->_host, $this ->_username, $this ->_password, true, $this ->CLIENT_MULTI_RESULTS ); if (! $this ->_dbc_handle) { $this ->alert( 'Can\'t connect to Server [ ' . $this ->_username. '@' . $this ->_host. ' ]' ); return false; } // 选择数据库 if (!mysql_select_db( $this ->_database, $this ->_dbc_handle)) { $this ->alert( 'Can\'t select database [' . $this ->_database. ']' ); return false; } $version = mysql_get_server_info( $this ->_dbc_handle); // 设置数据库编码 if ( $version >= '4.1' ) { //使用UTF8存取数据库 需要mysql 4.1.0以上支持 mysql_query( 'SET NAMES "' . $this ->_charset. '"' , $this ->_dbc_handle); } //设置 sql_model if ( $version > '5.0.1' ) { mysql_query( 'SET SQL_Mode=""' , $this ->_dbc_handle); } return true; } // 释放数据查询 private function free() { $this ->trace( 'public::query::free' ); if ( $this ->_query_handle && $this ->_operate == 'SELECT' ) { mysql_free_result( $this ->_query_handle); } unset( $this ->_query_handle); unset( $this ->_operate); unset( $this ->_column); unset( $this ->_where); unset( $this ->_order); unset( $this ->_limit); unset( $this ->_data); unset( $this ->_cache); unset( $this ->_result); return true; } // 关闭数据库连接 private function close() { if ( $this ->_dbc_handle) { $this ->trace( 'public::server::close' ); mysql_close( $this ->_dbc_handle); unset( $this ->_dbc_handle); } } // <![数据库操作][ // 增改删查 public function select( $column ) { $this ->_operate = 'SELECT' ; $this ->_column = $column ; return $this ; } public function update( $column ) { $this ->_operate = 'UPDATE' ; $this ->_column = $column ; return $this ; } public function insert( $column ) { $this ->_operate = 'INSERT' ; $this ->_column = $column ; return $this ; } public function delete ( $column ) { $this ->_operate = 'DELETE' ; $this ->_column = $column ; return $this ; } // 条件 public function where( $where ) { $this ->_where[] = $where ; return $this ; } // 排序 public function order( $order ) { $this ->_order[] = $order ; return $this ; } // 限制返回结果数 public function limit( $limit ) { $this ->_limit = $limit ; return $this ; } // 数据存储 public function data( $data ) { $this ->_data[] = $data ; return $this ; } // 缓存设置 public function cache( $cache ) { $this ->_cache = $cache ; return $this ; } // 开始执行操作 public function done() { $this ->trace( 'public::query::init' ); // 数据表 $column = $this ->_prefix. $this ->_column; // 组合SQL switch ( $this ->_operate) { case 'SELECT' : $sql = 'SELECT * FROM `' . $column . '`' . $this ->pack_where(). $this ->pack_order(). $this ->pack_limit(); break ; case 'UPDATE' : $sql = 'UPDATE `' . $column . '`' . $this ->pack_data(). $this ->pack_where(); break ; case 'INSERT' : $sql = 'INSERT INTO `' . $column . '`' . $this ->pack_data(); break ; case 'DELETE' : $sql = 'DELETE FROM `' . $column . '`' . $this ->pack_where(); break ; default : break ; } $this ->sql = $sql ; // 缓存判断 [暂时只支持缓存查询] if ( $this ->_operate == 'SELECT' && $this ->cache_check()) { $return = $this ->_result; // 清理变量池并返回 if ( $this ->free()) return $return ; } // 连接判断 if (! $this ->_dbc_handle) $this ->connect(); // 开始执行SQL $this ->trace( 'public::query::begin[' . $this ->_operate. ']' ); $this ->_query_handle = mysql_query( $sql , $this ->_dbc_handle); if (! $this ->_query_handle) { $this ->alert( 'SQL run error.' ); } $this ->trace( 'public::query::finish[' . $this ->_operate. ']' ); if ( $this ->_operate == 'SELECT' ) { if (mysql_num_rows( $this ->_query_handle) > 0) { while ( $one_row = mysql_fetch_assoc( $this ->_query_handle)) { $this ->_result[] = $one_row ; } mysql_data_seek( $this ->_query_handle, 0); } else { $this ->_result = null; } // 写缓存 if ( $this ->_need_cache) $this ->cache_write(); $return = $this ->_result; // 清理变量池并返回 if ( $this ->free()) return $return ; } else { $return = mysql_affected_rows( $this ->_dbc_handle); // 清理变量池并返回 if ( $this ->free()) return $return ; } } // 返回结果限制 private function pack_limit() { if ( $this ->_limit == '' ) return '' ; if ( is_numeric ( $this ->_limit)) { return ' LIMIT 0,' . $this ->_limit; } elseif ( is_string ( $this ->_limit)) { return ' LIMIT ' . $this ->_limit; } } // 条件整合 private function pack_where() { if (! $this ->_where) return '' ; $sql_where = ' WHERE ' ; foreach ( $this ->_where as $where ) { if ( is_array ( $where )) { foreach ( $where as $key => $val ) { if ( is_numeric ( $val )) { $sql_where .= $key . '=' . $val ; } elseif ( is_string ( $val )) { $sql_where .= $key . '="' . $val . '"' ; } $sql_where .= ' and ' ; } } elseif ( is_string ( $where )) { $conds = explode ( ',' , $where ); foreach ( $conds as $one_cond ) { $sql_where .= $one_cond . ' and ' ; } } } return substr ( $sql_where , 0, -5); } // 排序整合 private function pack_order() { if (! $this ->_order) return '' ; $sql_order = ' ORDER BY ' ; foreach ( $this ->_order as $order ) { if ( is_array ( $order )) { foreach ( $order as $key => $type ) { $sql_order .= $key . ' ' . $type . ', ' ; } } elseif ( is_string ( $order )) { $ords = explode ( ',' , $order ); foreach ( $ords as $one_ord ) { $sql_order .= str_replace ( '.' , ' ' , $one_ord ). ', ' ; } } } return substr ( $sql_order , 0, -2); } // 数据整合 private function pack_data() { if (! $this ->_data) return '' ; $sql_data = ' SET ' ; foreach ( $this ->_data as $data ) { if ( is_array ( $data )) { foreach ( $data as $key => $val ) { if ( is_numeric ( $val )) { $sql_data .= $key . '=' . $val ; } elseif ( is_string ( $val )) { $sql_data .= $key . '="' . $val . '"' ; } $sql_data .= ', ' ; } } elseif ( is_string ( $data )) { $datas = explode ( ',' , $data ); foreach ( $datas as $one_data ) { $sql_data .= $one_data . ', ' ; } } } return substr ( $sql_data , 0, -2); } // ]> // 缓存检测 private function cache_check() { $this ->trace( 'private::cache::check' ); if ( $this ->_cache == '' ) return false; $this ->_cache_key = md5( $this ->sql. '@' . $this ->CACHE_HASH_SALT); $time_calc = array ( 's' => 1, 'm' => 60, 'h' => 3600, 'd' => 86400 ); $c_rule = explode ( ':' , $this ->_cache); $c_time = $c_rule [0]; $c_long = (int) $c_rule [1]; if (time() - $this ->cache_time() > $time_calc [ $c_time ]* $c_long ) { $this ->_need_cache = true; return false; } $this ->_result = $this ->cache_read(); return true; } // 获取时间 private function cache_time() { $handle = 'cache_handle_' . $this ->_cached. '_time' ; return $this -> $handle ( $this ->_cache_key); } // 读缓存 private function cache_read() { $this ->trace( 'private::cache::read' ); $handle = 'cache_handle_' . $this ->_cached. '_value' ; return $this -> $handle ( $this ->_cache_key); } // 写缓存 private function cache_write() { $this ->trace( 'private::cache::write' ); $handle = 'cache_handle_' . $this ->_cached. '_write' ; $this -> $handle ( $this ->_cache_key, $this ->_result); $this ->_need_cache = false; } // <![缓存方式][ // 文件缓存 private function cache_handle_file_time( $key ) { if ( is_file ( $this ->_fc_path. $key . '.sql' )) { return filemtime ( $this ->_fc_path. $key . '.sql' ); } else { return 0; } } private function cache_handle_file_value( $key ) { if ( is_file ( $this ->_fc_path. $key . '.sql' )) { return unserialize( file_get_contents ( $this ->_fc_path. $key . '.sql' )); } else { return false; } } private function cache_handle_file_write( $key , $val ) { file_put_contents ( $this ->_fc_path. $key . '.sql' , serialize( $val )); return true; } // memcache 缓存 [这里做的不怎么好,不过平常也用不到memcache的 ^_^] private function cache_handle_memcache_time( $key ) { $mec = new Memcache(); $mec ->connect( $this ->_mc_server); $val = $mec ->get( $this ->_cache_key. '_time' ); $mec ->close(); if ( $val == '' ) { return 0; } else { return $val ; } } private function cache_handle_memcache_value( $key ) { $mec = new Memcache(); $mec ->connect( $this ->_mc_server); $val = $mec ->get( $this ->_cache_key. '_value' ); $mec ->close(); if ( $val == '' ) { return false; } else { return $val [ 'value' ]; } } private function cache_handle_memcache_write( $key , $val ) { $mec = new Memcache(); $mec ->connect( $this ->_mc_server); $mec ->set( $this ->_cache_key. '_time' , time()); $mec ->set( $this ->_cache_key. '_value' , array ( 'cached' =>true, 'value' => $val )); $mec ->close(); return true; } // ]> // <![一些数据库维护,例如:清空删除数据库、表,数据表优化等][ // 功能后续添加 // ]> // 调试信息 private function alert( $message ) { if (! $this ->_debug) return ; echo '<div style="border:2px solid #000;margin:10px;padding:10px;">' ; echo $message ; echo '<hr/>' ; echo mysql_error(); echo '</div>' ; exit ; } // 记录调试 private function trace( $message ) { if (! $this ->_debug) return ; $this ->_trace[] = array ( 'timer' =>microtime(), 'mmusage' =>memory_get_usage(), 'message' => $message ); } // 输出调试 public function trace_output() { if (! $this ->_debug) return ; echo '<div style="border:2px solid #000;margin:10px;padding:10px;">' ; echo '<ul>' ; foreach ( $this ->_trace as $i => $trace ) { $timer_e = explode ( ' ' , $trace [ 'timer' ]); $timer = (float) $timer_e [0]; $mmusage = $trace [ 'mmusage' ]; echo '<li>Time: ' . $timer . ' <font color="#0FC69D">+' .( $timer - $last_timer ). '</font> Memory: ' . $trace [ 'mmusage' ]. ' <font color="#E56298">+' .( $mmusage - $last_mmusage ). '</font> Call: ' . $trace [ 'message' ]. '</li>' ; $last_timer = $timer ; $last_mmusage = $mmusage ; } echo '</ul>' ; echo '</div>' ; } } ?> |
具体使用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | <?php include 'class.mysql.php' ; echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' ; $conf_info = array ( // 开启调试 'debug' =>true, // MySQL主机 'host' => 'localhost:3306' , // 用户 'username' => 'run' , // 密码 'password' => 'moyo' , // 数据库 'database' => 'test' , // 数据表前缀 'prefix' => '' , // 数据库编码 'charset' => 'utf8' , // 缓存方式 // memcache缓存协议,“://” 后面的是服务器地址 //'cached'=>'memcache://127.0.0.1:11211' // 文本缓存协议,“://”后面的是缓存地址。可用的标记:{root} 站点根目录, {current} 当前脚本目录 'cached' => 'file://{current}/query_cache/' ); // 获取实例 $dbc = db_mysql::getInstance(); // 载入配置 [只支持数组方式] $dbc ->config( $conf_info ); $start = explode ( ' ' , microtime()); $memory_start = memory_get_usage(); // 插入数据 $affect = $dbc // 操作表:user ->insert( 'user' ) // 支持数组方式 ->data( array ( 'name' => "Moyo" )) // 支持字符方式 ->data( 'mail="moyo@mail"' ) ->done(); echo $dbc ->sql; echo '<p></p>' ; echo 'INSERT 操作完成,影响行数:' . $affect ; echo '<hr/>' ; // 修改数据 $affect = $dbc ->update( 'user' ) // 支持数组方式 ->where( array ( 'name' => 'Moyo' )) // 支持字符方式 ->where( 'mail="moyo@mail"' ) ->data( array ( 'name' => "Moyo.live" , 'mail' => 'moyo@uuland' )) ->done(); echo $dbc ->sql; echo '<p></p>' ; echo 'UPDATE 操作完成,影响行数:' . $affect ; echo '<hr/>' ; // 获取数据 $result = $dbc ->select( 'user' ) ->where( 'name like "%Moyo%"' ) ->order( 'id.desc' ) ->limit(3) // 使用缓存,有效时间:10秒 [单位支持:d 天,h 时, m 分, s 秒] ->cache( 's:10' ) ->done(); echo $dbc ->sql; echo '<p></p>' ; echo 'SELECT 完成,记录数:' . count ( $result ); echo '<pre>' ; print_r( $result ); echo '</pre>' ; echo '<hr/>' ; // 删除数据 $affect = $dbc -> delete ( 'user' ) ->where( 'name="Moyo.live"' ) ->done(); echo $dbc ->sql; echo '<p></p>' ; echo 'DELETE 操作完成,影响行数:' . $affect ; echo '<hr/>' ; $finish = explode ( ' ' , microtime()); $memory_finish = memory_get_usage(); $start = $start [1]+ $start [0]; $finish = $finish [1]+ $finish [0]; $time = $finish - $start ; $memory = $memory_finish - $memory_start ; echo 'Trace (Time use ' . $time . ' Sec , Memory use ' . $memory_finish . ' Bytes , Incress ' . $memory . ' Bytes)' ; echo '<hr width="30%" align="left" />' ; $dbc ->trace_output(); ?> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步