支持php4、php5的mysql数据库操作类

6年以前  |  阅读数:1047 次  |  编程语言:PHP 

前端一直使用PHP5,的确使用起来特别的爽,现在为了能在俺的虚拟主机上跑,不得不改成PHP4的。这几个库类我以前发在PHPCHIAN,地址是http://www.phpchina.com/bbs/viewthread.php?tid=5687&highlight;=。(前几天在网上搜索了下,发现很多转载我的这几篇文章都没有说明出处,而且把我的版权都删除了,气晕了。)

昨天改写了数据库操作类,恰好在我简化zend Framework也能用到。   

代码如下:   

<?php
/**

在此基础上,我顺便封装SIDU(select,insert,update,delete)四种基本操作,作为简化的zend Framework的module。代码如下(这个没写注释了,懒的写。。):

<?
class module{

var $mysql;

var $tbname;

var $debug=false;

function __construct($tbname){
if(!is_string($tbname))die('Module need a args of tablename');
$this->tbname=$tbname;
$this->mysql=phpbean::registry('db');
}

function _setDebug($debug=true){
$this->debug=$debug;
}

function add($row){
if(!is_array($row))die('module error:row should be an array');
$strsql='insert into '.$this->tbname.'';
$keys='';
$values='';
foreach($row as $key=>$value){
$keys.=''.$key.',';
$values.='\''.$value.'\'';
}
$keys=rtrim($keys,',');
$values=rtrim($values,',');
$strsql.=' ('.$keys.') values ('.$values.')';
if($this->debug)echo '


'.$strsql.'
';
$this->mysql->query($strsql);
return $this->mysql->insert_id();
}

function query($strsql){
return $this->mysql->Execute($strsql);
}

function count($where=''){
$strsql='select count(*) as num from '.$this->tbname.' ';
if(!empty($where))$strsql.=$where;
$rs=$this->mysql->Execute($strsql);
return $rs[0]['num'];
}

function select($where=''){
$strsql='select * from '.$this->tbname.' ';
if(!empty($where))$strsql.=$where;
return $this->mysql->Execute($strsql);
}

function delete($where=''){
if(empty($where))die('Error:the delete method need a condition!');
return $this->mysql->query('delete from '.$this->tbname.' '.$where);
}

function update($set,$where){
if(empty($where))die('Error:the update method need a condition!');
if(!is_array($set))die('Error:Set must be an array!');
$strsql='update '.$this->tbname.' ';
//get a string of set
$strsql.='set ';
foreach($set as $key=>$value){
$strsql.=''.$key.'=\''.$value.'\',';
}
$strsql=rtrim($strsql,',');
return $this->mysql->query($strsql.' '.$where);
}

function detail($where){
if(empty($where))die('Error:where should not empty!');
$rs=$this->mysql->query('select * from '.$this->tbname.' '.$where);
return $this->mysql->seek(0);
}
}
?>

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8