自己練練手,寫一個一個功能比較簡單的MySQL數據庫操作類
<?php
/**
* Created by JetBrains PhpStorm.
* User:JAE
* Date: 13-8-13
* Time: 下午5:15
* Blog:http://blog.jaekj.com
* QQ:734708094
* 通用數據庫操作類
* 版本:V1.1
*/
/* 數據庫配置
return array(
'DB_CONFIG' => array(
//數據庫配置
'DB_HOST'=>'127.0.0.1', //服務器地址
'DB_NAME' => 'tmp', // 數據庫名
'DB_USER' => 'root', // 用戶名
'DB_PWD' => '', // 密碼
'DB_ENCODE'=>'utf8',//編碼
'DB_PREFIX' => 'dmtx_' // 數據庫表前綴
)
);
*/
class M
{
private $link; //數據庫連接
private $table; //表名
private $prefix; //表前綴
private $db_config; //數據庫配置
/**
* 參數:表名 數據庫配置數組 或 數據庫配置文件路徑
* @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;
}
/**
* 連接數據庫
*/
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('數據庫服務器連接錯誤:' . mysql_error());
mysql_select_db($db_name) or die('數據庫連接錯誤:' . mysql_error());
mysql_query("set names '$db_encode'");
}
/**
* 數據查詢
* 參數:sql條件 查詢字段 使用的sql函數名
* @param string $where
* @param string $field
* @param string $fun
* @return array
* 返回值:結果集 或 結果(出錯返回空字符串)
*/
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;
}
/**
* 數據更新
* 參數:sql條件 要更新的數據(字符串 或 關聯數組)
* @param $where
* @param $data
* @return bool
* 返回值:語句執行成功或失敗,執行成功並不意味著對數據庫做出了影響
*/
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);
}
/**
* 數據添加
* 參數:數據(數組 或 關聯數組 或 字符串)
* @param $data
* @return int
* 返回值:插入的數據的ID 或者 0
*/
public function insert($data)
{
$field = '';
$idata = '';
if (is_array($data) && array_keys($data) != range(0, count($data) - 1)) {
//關聯數組
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 {
//非關聯數組 或字符串
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;
}
/**
* 數據刪除
* 參數:sql條件
* @param $where
* @return bool
*/
public function delete($where)
{
$sqlStr = "delete from $this->table where $where";
return mysql_query($sqlStr);
}
/**
* 關閉MySQL連接
* @return bool
*/
public function close()
{
return mysql_close($this->link);
}
}
//$hj = new M("user");
//echo $hj->insert("NULL,'hj.q@qq.com','cde'");
//$arr = $hj->select();
//print_r($arr);
//echo $hj->update("id>3",array("email"=>"bn@c.cc"));

<?php
require 'Query/M.class.php';
$hj = new M("user");
//增
echo $hj->insert(array("NULL","1@qq.com","xxx"));
echo $hj->insert(array("email"=>"12@qq.com","password"=>"cccc"));
echo $hj->insert("NULL,'123@qq.com','cde'");
//查
//查看所有數據
$arr = $hj->select();
print_r($arr);
//查看id大於3且id小於6 的id 和email 字段 的所有數據
$arr = $hj->select("id>3 and id<6","id,email");
print_r($arr);
//獲取數據庫中一共有多少條記錄
$rt = $hj->select("1","*","count");
echo $rt;
// 改
$b = $hj->update("id=1","email = '1234@qq.com'");
echo $b;
$b = $hj->update("id>3 and id<6",array("email"=>"1234@qq.com"));
echo $b;
// 刪
$b = $hj->delete("id>3");
echo $b;
//關閉
$hj->close();