<?php /** * Created by JetBrains PhpStorm. * User:JAE * Date: 13-8-13 * Time: 下午5:15 * Blog:http://blog. * QQ:734708094 * 通用數(shù)據(jù)庫(kù)操作類 * 版本:V1.1 */ /* 數(shù)據(jù)庫(kù)配置 return array( 'DB_CONFIG' => array( //數(shù)據(jù)庫(kù)配置 'DB_HOST'=>'127.0.0.1', //服務(wù)器地址 'DB_NAME' => 'tmp', // 數(shù)據(jù)庫(kù)名 'DB_USER' => 'root', // 用戶名 'DB_PWD' => '', // 密碼 'DB_ENCODE'=>'utf8',//編碼 'DB_PREFIX' => 'dmtx_' // 數(shù)據(jù)庫(kù)表前綴 ) ); */ class M { private $link; //數(shù)據(jù)庫(kù)連接 private $table; //表名 private $prefix; //表前綴 private $db_config; //數(shù)據(jù)庫(kù)配置 /** * 參數(shù):表名 數(shù)據(jù)庫(kù)配置數(shù)組 或 數(shù)據(jù)庫(kù)配置文件路徑 * @param $table * @param string $db_config_arr_path */ function __construct($table, $db_config_arr_path = 'config.php') { if (is_array($db_config_arr_path)) { $this->db_config = $db_config_arr_path; } else { $this->db_config = require($db_config_arr_path); } $this->conn(); $this->table = $this->prefix . $table; } /** * 連接數(shù)據(jù)庫(kù) */ private function conn() { $db_config = $this->db_config; $host = $db_config["DB_CONFIG"]["DB_HOST"]; $user = $db_config["DB_CONFIG"]["DB_USER"]; $pwd = $db_config["DB_CONFIG"]["DB_PWD"]; $db_name = $db_config["DB_CONFIG"]["DB_NAME"]; $db_encode = $db_config["DB_CONFIG"]["DB_ENCODE"]; $this->prefix = $db_config["DB_CONFIG"]["DB_PREFIX"]; $this->link = mysql_connect($host, $user, $pwd) or die('數(shù)據(jù)庫(kù)服務(wù)器連接錯(cuò)誤:' . mysql_error()); mysql_select_db($db_name) or die('數(shù)據(jù)庫(kù)連接錯(cuò)誤:' . mysql_error()); mysql_query("set names '$db_encode'"); } /** * 數(shù)據(jù)查詢 * 參數(shù):sql條件 查詢字段 使用的sql函數(shù)名 * @param string $where * @param string $field * @param string $fun * @return array * 返回值:結(jié)果集 或 結(jié)果(出錯(cuò)返回空字符串) */ public function select($where = '1', $field = "*", $fun = '') { $rarr = array(); if (empty($fun)) { $sqlStr = "select $field from $this->table where $where"; $rt = mysql_query($sqlStr, $this->link); while ($rt && $arr = mysql_fetch_assoc($rt)) { array_push($rarr, $arr); } } else { $sqlStr = "select $fun($field) as rt from $this->table where $where"; $rt = mysql_query($sqlStr, $this->link); if ($rt) { $arr = mysql_fetch_assoc($rt); $rarr = $arr['rt']; } else { $rarr = ''; } } return $rarr; } /** * 數(shù)據(jù)更新 * 參數(shù):sql條件 要更新的數(shù)據(jù)(字符串 或 關(guān)聯(lián)數(shù)組) * @param $where * @param $data * @return bool * 返回值:語(yǔ)句執(zhí)行成功或失敗,執(zhí)行成功并不意味著對(duì)數(shù)據(jù)庫(kù)做出了影響 */ public function update($where, $data) { $ddata = ''; if (is_array($data)) { while (list($k, $v) = each($data)) { if (empty($ddata)) { $ddata = "$k='$v'"; } else { $ddata .= ",$k='$v'"; } } } else { $ddata = $data; } $sqlStr = "update $this->table set $ddata where $where"; return mysql_query($sqlStr); } /** * 數(shù)據(jù)添加 * 參數(shù):數(shù)據(jù)(數(shù)組 或 關(guān)聯(lián)數(shù)組 或 字符串) * @param $data * @return int * 返回值:插入的數(shù)據(jù)的ID 或者 0 */ public function insert($data) { $field = ''; $idata = ''; if (is_array($data) && array_keys($data) != range(0, count($data) - 1)) { //關(guān)聯(lián)數(shù)組 while (list($k, $v) = each($data)) { if (empty($field)) { $field = "$k"; $idata = "'$v'"; } else { $field .= ",$k"; $idata .= ",'$v'"; } } $sqlStr = "insert into $this->table($field) values ($idata)"; } else { //非關(guān)聯(lián)數(shù)組 或字符串 if (is_array($data)) { while (list($k, $v) = each($data)) { if (empty($idata)) { $idata = "'$v'"; } else { $idata .= ",'$v'"; } } } else { //為字符串 $idata = $data; } $sqlStr = "insert into $this->table values ($idata)"; } if(mysql_query($sqlStr,$this->link)) { return mysql_insert_id($this->link); } return 0; } /** * 數(shù)據(jù)刪除 * 參數(shù):sql條件 * @param $where * @return bool */ public function delete($where) { $sqlStr = "delete from $this->table where $where"; return mysql_query($sqlStr); } /** * 關(guān)閉MySQL連接 * @return bool */ public function close() { return mysql_close($this->link); } } |
|