前端一直使用PHP5,的确使用起来特别的爽,现在为了能在俺的虚拟主机上跑,不得不改成PHP4的。这几个库类我以前发在PHPCHIAN,地址是http://www.phpchina.com/bbs/viewthread.php?tid=5687&highlight;=。(前几天在网上搜索了下,发现很多转载我的这几篇文章都没有说明出处,而且把我的版权都删除了,气晕了。)
昨天改写了数据库操作类,恰好在我简化zend Framework也能用到。
代码如下:
<?php
/**
filename: DB_Mysql.class.php
@package:phpbean
@author :feifengxlq<[email]feifengxlq@gmail.com[/email]>
@copyright :Copyright 2006 feifengxlq
@license:version 1.2
create:2006-5-30
modify:2006-10-19 by feifengxlq
description:the interface of mysql.
example:
////////////Select action (First mode)//////////////////////////////
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->query("select * from test");
for($i=0;$i<$mysql->num_rows($rs);$i++)
$record[$i]=$mysql->seek($i);
print_r($record);
$mysql->close();
////////////Select action (Second mode)//////////////////////////////
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->execute("select * from test");
print_r($rs);
$mysql->close();
/////////////insert action////////////////////////////
$mysql=new DB_Mysql("localhost","root","root","root");
$mysql->query("insert into test(username) values('test from my DB_mysql')");
printf("%s",$mysql->insert_id());
$mysql->close();
*/
class mysql{
/ private: connection parameters /
var $host="localhost";
var $database="";
var $user="root";
var $password="";
/ private: configuration parameters /
var $pconnect=false;
var $debug=false;
/ private: result array and current row number /
var $link_id=0;
var $query_id=0;
var $record=array();
/**
/**
/**
/**
/**
/**
*/
function free()
{
if(($this->query_id)&($this->query_id!=0))@mysql_free_result($this->query_id);
}
/**
function num_rows()
{
return @mysql_num_rows($this->query_id);
}
function num_fields()
{
return @mysql_num_fields($this->query_id);
}
function insert_id()
{
return @mysql_insert_id($this->link_id);
}
function close()
{
$this->free();
if($this->link_id!=0)@mysql_close($this->link_id);
if(mysql_errno()!=0) die("Mysql Error:".mysql_errno().":".mysql_error());
}
function select($strsql,$number,$offset)
{
if(empty($number)){
return $this->Execute($strsql);
}else{
return $this->Execute($strsql.' limit '.$offset.','.$number);
}
}
function __destruct()
{
$this->close();
$this->set("user","");
$this->set("host","");
$this->set("password","");
$this->set("database","");
}
}
?>
在此基础上,我顺便封装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 '
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);
}
}
?>