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

前端一直使用PHP5,的确使用起来特别的爽,现在为了能在俺的虚拟主机上跑,不得不改成PHP4的。这几个库类我以前发在PHPCHIAN,地址是http://www.phpchina.com/bbs/viewthread.php?tid=5687&highlight=。(前几天在网上搜索了下,发现很多转载我的这几篇文章都没有说明出处,而且把我的版权都删除了,气晕了。) 昨天改写了数据库操作类,恰好在我简化zendFramework也能用到。 代码如下:
<?php
/
filename:DB_Mysql.class.php
@package:phpbean
@author:feifengxlq<[email]feifengxlq@gmail.com[/email]>
@copyright:Copyright2006feifengxlq
@license:version1.2
create:2006-5-30
modify:2006-10-19byfeifengxlq
description:theinterfaceofmysql.

example:
////////////Selectaction(Firstmode)//////////////////////////////
$mysql=newDB_Mysql("localhost","root","root");
$rs=$mysql->query("select
fromtest");
for($i=0;$i<$mysql->num_rows($rs);$i++)
$record[$i]=$mysql->seek($i);
print_r($record);
$mysql->close();
////////////Selectaction(Secondmode)//////////////////////////////
$mysql=newDB_Mysql("localhost","root");
$rs=$mysql->execute("select
fromtest");
print_r($rs);
$mysql->close();
/////////////insertaction////////////////////////////
$mysql=newDB_Mysql("localhost","root");
$mysql->query("insertintotest(username)values('testfrommyDB_mysql')");
printf("%s",$mysql->insert_id());
$mysql->close();
/
classmysql{ /private:connectionparameters/
var$host="localhost";
var$database="";
var$user="root";
var$password=""; /private:configurationparameters/
var$pconnect=false;
var$debug=false; /private:resultarrayandcurrentrownumber/
var$link_id=0;
var$query_id=0;
var$record=array(); /

construct

@paramstring$host
@paramstring$user
@paramstring$password
@paramstring$database
*/
functionconstruct($host="localhost",$user="root",$password="",$database="")
{
$this->set("host",$host);
$this->set("user",$user);
$this->set("password",$password);
$this->set("database",$database);
$this->connect();
} /*
setthevaluefortheparamofthisclass

@paramstring$var
@paramstring$value
/
functionset($var,$value)
{
$this->$var=$value;
}
/*
connecttoamysqlserver,andchoosethedatabase.

@paramstring$database
@paramstring$host
@paramstring$user
@paramstring$password
@returnlink_id
*/
functionconnect($database="",$host="",$user="",$password="")
{
if(!empty($database))$this->set("database",$database);
if(!empty($host))$this->set("host",$host);
if(!empty($user))$this->set("user",$user);
if(!empty($password))$this->set("password",$password);
if($this->link_id==0)
{
if($this->pconnect)
$this->link_id=@mysql_pconnect($this->host,$this->user,$this->password);
else
$this->link_id=@mysql_connect($this->host,$this->password);
if(!$this->link_id)
die("MysqlConnectErrorin".FUNCTION."():".mysql_errno().":".mysql_error());
if(!@mysql_select_db($this->database,$this->link_id))
die("MysqlSelectdatabaseErrorin".
FUNCTION."():".mysql_errno().":".mysql_error());
}
return$this->link_id;
} /*
queryasqlintothedatabase

@paramstring$strsql
@returnquery_id
/
functionquery($strsql="")
{
if(empty($strsql))die("MysqlError:".
FUNCTION."()strsqlisempty!");
if($this->link_id==0)$this->connect();
if($this->debug)printf("Debugquerysql:%s",$strsql);
$this->query_id=@mysql_query($strsql,$this->link_id);
if(!$this->query_id)die("Mysqlqueryfail,Invalidsql:".$strsql.".");
return$this->query_id;
} /
queryasqlintothedatabase,whileitisdiffererntfromthequery()method,
thismethodwillreturnarecord(array);

@paramstring$strsql
@paramstring$style
@return$recordisaarray()
*/
functionExecute($strsql,$style="array")
{
$this->query($strsql);
if(!empty($this->record))$this->record=array();
$i=0;
if($style=="array"){
while($temp=@mysql_fetch_array($this->query_id)){
$this->record[$i]=$temp;
$i++;
}
}else{
while($temp=@mysql_fetch_object($this->query_id)){
$this->record[$i]=$temp;
$i++;
}
}
unset($i);
unset($temp);
return$this->record;
} /
seek,butnotequaltomysql_data_seek.thismethordwillreturnalist.

@paramint$pos
@paramstring$style
@returnrecord
/
functionseek($pos=0,$style="array")
{
if(!@mysql_data_seek($this->query_id,$pos))
die("Errorin".FUNCTION."():cannotseektorow".$pos."!");
$result=@($style=="array")?mysql_fetch_array($this->query_id):mysql_fetch_object($this->query_id);
if(!$result)die("Errorin".FUNCTION."():cannotfetchdata!");
return$result;
}
/

freetheresultofquery

*/
functionfree()
{
if(($this->query_id)&($this->query_id!=0))@mysql_free_result($this->query_id);
} /

evaluatetheresult(size,width)

@returnnum
/
functionaffected_rows()
{
return@mysql_affected_rows($this->link_id);
} functionnum_rows()
{
return@mysql_num_rows($this->query_id);
} functionnum_fields()
{
return@mysql_num_fields($this->query_id);
} functioninsert_id()
{
return@mysql_insert_id($this->link_id);
} functionclose()
{
$this->free();
if($this->link_id!=0)@mysql_close($this->link_id);
if(mysql_errno()!=0)die("MysqlError:".mysql_errno().":".mysql_error());
} functionselect($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)四种基本操作,作为简化的zendFramework的module。代码如下(这个没写注释了,懒的写。。):
<?
classmodule{ var$mysql; var$tbname; var$debug=false; function__construct($tbname){
if(!is_string($tbname))die('Moduleneedaargsoftablename');
$this->tbname=$tbname;
$this->mysql=phpbean::registry('db');
} function_setDebug($debug=true){
$this->debug=$debug;
} functionadd($row){
if(!is_array($row))die('moduleerror:rowshouldbeanarray');
$strsql='insertinto'.$this->tbname.'';
$keys='';
$values='';
foreach($rowas$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();
} functionquery($strsql){
return$this->mysql->Execute($strsql);
} functioncount($where=''){
$strsql='selectcount()asnumfrom'.$this->tbname.'';
if(!empty($where))$strsql.=$where;
$rs=$this->mysql->Execute($strsql);
return$rs[0]['num'];
} functionselect($where=''){
$strsql='select
from'.$this->tbname.'';
if(!empty($where))$strsql.=$where;
return$this->mysql->Execute($strsql);
} functiondelete($where=''){
if(empty($where))die('Error:thedeletemethodneedacondition!');
return$this->mysql->query('deletefrom'.$this->tbname.''.$where);
} functionupdate($set,$where){
if(empty($where))die('Error:theupdatemethodneedacondition!');
if(!is_array($set))die('Error:Setmustbeanarray!');
$strsql='update'.$this->tbname.'';
//getastringofset
$strsql.='set';
foreach($setas$key=>$value){
$strsql.=''.$key.'=\''.$value.'\',';
}
$strsql=rtrim($strsql,');
return$this->mysql->query($strsql.''.$where);
} functiondetail($where){
if(empty($where))die('Error:whereshouldnotempty!');
$rs=$this->mysql->query('select*from'.$this->tbname.''.$where);
return$this->mysql->seek(0);
}
}
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


如何选择合适的 C++ Web 开发框架?
利用 C++ 框架构建高并发 Web 应用的策略
用 C++ 框架开发跨平台图形应用程序
golang框架中安全编码实践的最佳指南是什么?
golang框架与其他语言框架在设计理念上的区别有哪些?
C++ 图形框架与其他语言框架的比较
C++ 框架与其他 Web 开发框架的对比分析
使用 C++ 框架构建大型项目最佳实践
C++ 框架如何提高大型项目开发效率
C++ 框架中依赖注入的持续集成与部署工具
如何与社区协作和贡献到自定义 Golang 框架?
C++ 框架在大型项目中如何实现模块化开发
使用 C++ 框架开发跨平台 Web 应用
C++ 框架在大型项目中的优缺点
golang框架在性能上的优势体现在哪些方面?
C++ 框架在嵌入式系统内存优化中的优势
golang框架在人工智能与机器学习中的作用
如何扩展 Golang 框架以支持特定功能?
如何利用 Go Modules 和依赖项管理来自定义 Golang 框架?
Golang 框架中的性能优化技巧