MyPic DB 数据库类
可以应用于任何独里的程序里了
唯一要做的就是 传给它一个配置数组, 不依赖于其它外部函数
PHP代码:
$arr = array(
'db_host' => 'localhost',
'db_user' => 'root',
'db_name' => 'test',
'db_pwd' => 'root',
'db_prefix' => 'mp_'
);
$db = new dbmysql($arr);
$db->connect()->select_db();
现在用着非常顺手.
封装了一些方法
1. 获取一条纪录到数组
2. 获取多条纪录到数组
3. insert(关联数组), 支持多维数组批量插入
4. update(关联数组)
5. delete 纪录
6. 自动给field名加上 ` ` 号
7. 自动给value 加上 ' ' 号
8. 自动处进表前缀 直接使用 [email=#@_table]#@_table[/email] 代替 mp_table 这种形式
注: 没有处理 转义.
PHP代码:
// dbmysql
class dbmysql {
// 数据库配置
protected $_config = array(
'db_host' => 'localhost',
'db_user' => 'root',
'db_pwd' => 'root',
'db_name' => 'mp',
'db_lang' => 'utf8',
'db_prefix' => 'mp_'
);
// 查询SQL
protected $_sql = null;
// 当前连接
protected $_conn = null;
// 当前资源
protected $_rs = null;
// __construct
public function __construct($config=array()){
!extension_loaded('mysql') && die('mysql模块不支持!');
$this->config($config);
}
// config
public function config($config=array()){
$this->_config = array_merge($this->_config, $config);
}
// contenct
public function connect($host='', $user='', $pwd=''){
!$host && $host = $this->_config['db_host'];
!$user && $user = $this->_config['db_user'];
!$pwd && $pwd = $this->_config['db_pwd'];
$this->_conn = @mysql_connect($host, $user, $pwd) or die('数据库连接失败!');
!version_compare($this->version(), '4.1', '<' ) or die('MySQL数据库版本低于4.1,请升级你的MySQL数据库!');
return $this;
}
// select_db
public function select_db($db=''){
!$db = $this->_config['db_name'];
@mysql_select_db($db, $this->_conn) or die("{$db}:数据库尚未建立或者无法连接!");
mysql_query("SET NAMES '{$this->_config['db_lang']}'", $this->_conn);
return $this;
}
// getone
public function getone($sql){
$query = $this->query($sql);
return $this->fetch($query);
}
// getall
public function getall($sql){
$arr = array();
$query = $this->query($sql);
while($val = $this->fetch($query)){
$arr[] = $val;
}
return $arr;
}
// insert
public function insert($table, $data=array()){
// $table:table, $l2:array
$cols = array();
$vals = array();
$one = reset($data);
if(is_array($one)){ // 多条数据
$cols = implode(',', $this->deal_field(array_keys($one)));
foreach($data as $val){
$vals[] = '(' .implode(',', $this->deal_value($val)).')' ;
}
$vals = implode(',', $vals);
} else{ // 单条数据
$cols = implode(',', $this->deal_field(array_keys($data)));
$vals = '(' . implode(',', $this->deal_value($data)). ')';
}
// 生成SQL
$sql = "INSERT INTO {$this->deal_field($table)} ( {$cols} ) VALUES {$vals}";
$this->exec($sql, $bind);
return $this->insert_id();
}
// update(, $set, $where, $bind)
public function update($table,$data,$where){
$set = array();
foreach ($data as $col => $val) {
$set[] = $this->deal_field($col).' = '.$this->deal_value($val);
}
$set = implode(',', $set);
$where = $this->deal_where($where);
!empty($where) && $where = " WHERE {$where}";
// build the statement
$sql = "UPDATE {$this->deal_field($table)} SET {$set} {$where}";
$this->exec($sql);
return $this->affected_rows();
}
// delete
public function delete($table, $where){
$where = $this->deal_where($where);
!empty($where) && $where = " WHERE {$where}";
// build the statement
$sql = "DELETE FROM {$this->deal_field($table)} {$where}";
$this->exec($sql, $bind);
return $this->affected_rows();
}
// query
public function query($sql){
return $this->execute($sql, 'mysql_query');
}
// exec
public function exec($sql){
return $this->execute($sql, 'mysql_unbuffered_query');
}
// execute
private function execute($sql, $func){
!$this->_conn && $this->connect();
$this->_sql = $this->deal_prefix($sql);
// 执行查询
if(!$this->_rs = $func($this->_sql, $this->_conn)){
die("SQL Query Error:<br/>SQL:{$this->_sql}<br>{$this->error()}", $this->errno());
}
return $this->_rs;
}
// result
public function result($sql, $num=0){
$query = $this->query($sql);
return mysql_result($query, $num);
}
// fetch
public function fetch($query=null, $type=1){
!$query && $query = $this->_rs;
if($type == 0){
return mysql_fetch_object($query);
} else {
return mysql_fetch_array($query, $type);
}
}
// num_rows
function num_rows($query=null){
!$query && $query = $this->_rs;
return mysql_num_rows($query);
}
// affected_rows
function affected_rows($query=null){
return $this->_conn ? mysql_affected_rows($this->_conn) : mysql_affected_rows();
}
// insert_id
public function insert_id() {
return ($I1 = mysql_insert_id($this->_conn)) >= 0 ? $I1 : $this->result("SELECT last_insert_id();");
}
// error
public function error() {
return $this->_conn ? mysql_error($this->_conn) : mysql_error();
}
// errno
public function errno() {
return intval($this->_conn ? mysql_errno($this->_conn) : mysql_errno());
}
// version
public function version(){
return mysql_get_server_info($this->_conn);
}
// free
public function free(){
if (is_resource($this->_rs)) {
return mysql_free_result($this->_rs);
}
}
// close
public function close(){
if (is_resource($this->_conn)) {
return mysql_close($this->_conn);
}
}
//********************* deal ***************************** //
// deal_prefix
public function deal_prefix($sql=''){
if($sql && strpos($sql, '#@_')!==false){
$sql = str_replace('#@_', $this->_config['db_prefix'], $sql);
}
return $sql;
}
// deal_field
public function deal_field($str=''){
if(is_array($str)){ // 数组
foreach($str as & $val){
$val = self::deal_field($val);
}
return $str;
}
$str && $str = "`{$str}`";
return $str;
}
// deal_value
public function deal_value($str=''){
if(is_array($str)){ // 数组
foreach($str as & $val){
$val = self::deal_value($val);
}
return $str;
}
$str && $str = "'{$str}'";
return $str;
}
// deal_where
public function deal_where($where) {
if(is_array($where)) {
//判断 OR AND XOR
if(array_key_exists('_logic', $where)){
$logic = strtoupper($where['_logic']);
unset($where['_logic']);
} else {
$logic = 'AND';
}
foreach ($where as &$term) {
$term = '(' . $term . ')';
}
$where = implode(' '.$logic.' ', $where);
}
return $where;
}
}
可以应用于任何独里的程序里了
唯一要做的就是 传给它一个配置数组, 不依赖于其它外部函数
PHP代码:
$arr = array(
'db_host' => 'localhost',
'db_user' => 'root',
'db_name' => 'test',
'db_pwd' => 'root',
'db_prefix' => 'mp_'
);
$db = new dbmysql($arr);
$db->connect()->select_db();
现在用着非常顺手.
封装了一些方法
1. 获取一条纪录到数组
2. 获取多条纪录到数组
3. insert(关联数组), 支持多维数组批量插入
4. update(关联数组)
5. delete 纪录
6. 自动给field名加上 ` ` 号
7. 自动给value 加上 ' ' 号
8. 自动处进表前缀 直接使用 [email=#@_table]#@_table[/email] 代替 mp_table 这种形式
注: 没有处理 转义.
PHP代码:
// dbmysql
class dbmysql {
// 数据库配置
protected $_config = array(
'db_host' => 'localhost',
'db_user' => 'root',
'db_pwd' => 'root',
'db_name' => 'mp',
'db_lang' => 'utf8',
'db_prefix' => 'mp_'
);
// 查询SQL
protected $_sql = null;
// 当前连接
protected $_conn = null;
// 当前资源
protected $_rs = null;
// __construct
public function __construct($config=array()){
!extension_loaded('mysql') && die('mysql模块不支持!');
$this->config($config);
}
// config
public function config($config=array()){
$this->_config = array_merge($this->_config, $config);
}
// contenct
public function connect($host='', $user='', $pwd=''){
!$host && $host = $this->_config['db_host'];
!$user && $user = $this->_config['db_user'];
!$pwd && $pwd = $this->_config['db_pwd'];
$this->_conn = @mysql_connect($host, $user, $pwd) or die('数据库连接失败!');
!version_compare($this->version(), '4.1', '<' ) or die('MySQL数据库版本低于4.1,请升级你的MySQL数据库!');
return $this;
}
// select_db
public function select_db($db=''){
!$db = $this->_config['db_name'];
@mysql_select_db($db, $this->_conn) or die("{$db}:数据库尚未建立或者无法连接!");
mysql_query("SET NAMES '{$this->_config['db_lang']}'", $this->_conn);
return $this;
}
// getone
public function getone($sql){
$query = $this->query($sql);
return $this->fetch($query);
}
// getall
public function getall($sql){
$arr = array();
$query = $this->query($sql);
while($val = $this->fetch($query)){
$arr[] = $val;
}
return $arr;
}
// insert
public function insert($table, $data=array()){
// $table:table, $l2:array
$cols = array();
$vals = array();
$one = reset($data);
if(is_array($one)){ // 多条数据
$cols = implode(',', $this->deal_field(array_keys($one)));
foreach($data as $val){
$vals[] = '(' .implode(',', $this->deal_value($val)).')' ;
}
$vals = implode(',', $vals);
} else{ // 单条数据
$cols = implode(',', $this->deal_field(array_keys($data)));
$vals = '(' . implode(',', $this->deal_value($data)). ')';
}
// 生成SQL
$sql = "INSERT INTO {$this->deal_field($table)} ( {$cols} ) VALUES {$vals}";
$this->exec($sql, $bind);
return $this->insert_id();
}
// update(, $set, $where, $bind)
public function update($table,$data,$where){
$set = array();
foreach ($data as $col => $val) {
$set[] = $this->deal_field($col).' = '.$this->deal_value($val);
}
$set = implode(',', $set);
$where = $this->deal_where($where);
!empty($where) && $where = " WHERE {$where}";
// build the statement
$sql = "UPDATE {$this->deal_field($table)} SET {$set} {$where}";
$this->exec($sql);
return $this->affected_rows();
}
// delete
public function delete($table, $where){
$where = $this->deal_where($where);
!empty($where) && $where = " WHERE {$where}";
// build the statement
$sql = "DELETE FROM {$this->deal_field($table)} {$where}";
$this->exec($sql, $bind);
return $this->affected_rows();
}
// query
public function query($sql){
return $this->execute($sql, 'mysql_query');
}
// exec
public function exec($sql){
return $this->execute($sql, 'mysql_unbuffered_query');
}
// execute
private function execute($sql, $func){
!$this->_conn && $this->connect();
$this->_sql = $this->deal_prefix($sql);
// 执行查询
if(!$this->_rs = $func($this->_sql, $this->_conn)){
die("SQL Query Error:<br/>SQL:{$this->_sql}<br>{$this->error()}", $this->errno());
}
return $this->_rs;
}
// result
public function result($sql, $num=0){
$query = $this->query($sql);
return mysql_result($query, $num);
}
// fetch
public function fetch($query=null, $type=1){
!$query && $query = $this->_rs;
if($type == 0){
return mysql_fetch_object($query);
} else {
return mysql_fetch_array($query, $type);
}
}
// num_rows
function num_rows($query=null){
!$query && $query = $this->_rs;
return mysql_num_rows($query);
}
// affected_rows
function affected_rows($query=null){
return $this->_conn ? mysql_affected_rows($this->_conn) : mysql_affected_rows();
}
// insert_id
public function insert_id() {
return ($I1 = mysql_insert_id($this->_conn)) >= 0 ? $I1 : $this->result("SELECT last_insert_id();");
}
// error
public function error() {
return $this->_conn ? mysql_error($this->_conn) : mysql_error();
}
// errno
public function errno() {
return intval($this->_conn ? mysql_errno($this->_conn) : mysql_errno());
}
// version
public function version(){
return mysql_get_server_info($this->_conn);
}
// free
public function free(){
if (is_resource($this->_rs)) {
return mysql_free_result($this->_rs);
}
}
// close
public function close(){
if (is_resource($this->_conn)) {
return mysql_close($this->_conn);
}
}
//********************* deal ***************************** //
// deal_prefix
public function deal_prefix($sql=''){
if($sql && strpos($sql, '#@_')!==false){
$sql = str_replace('#@_', $this->_config['db_prefix'], $sql);
}
return $sql;
}
// deal_field
public function deal_field($str=''){
if(is_array($str)){ // 数组
foreach($str as & $val){
$val = self::deal_field($val);
}
return $str;
}
$str && $str = "`{$str}`";
return $str;
}
// deal_value
public function deal_value($str=''){
if(is_array($str)){ // 数组
foreach($str as & $val){
$val = self::deal_value($val);
}
return $str;
}
$str && $str = "'{$str}'";
return $str;
}
// deal_where
public function deal_where($where) {
if(is_array($where)) {
//判断 OR AND XOR
if(array_key_exists('_logic', $where)){
$logic = strtoupper($where['_logic']);
unset($where['_logic']);
} else {
$logic = 'AND';
}
foreach ($where as &$term) {
$term = '(' . $term . ')';
}
$where = implode(' '.$logic.' ', $where);
}
return $where;
}
}
作者:jane@淘宝网女装新款秋装连衣裙裤子外套上衣_2012时尚女装新款 Ecmall二次开发-PHP技术
地址:http://www.laohucheng.com/post/318/
版权所有©转载时必须以链接形式注明作者和原始出处及本声明!
MyPic 图片加水印, 生成缩略图类
生成签名图片代码
2009
23:50
490
0


