热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

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

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

代码如下:


/**
* 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();

/**
* construct
*
* @param string $host
* @param string $user
* @param string $password
* @param string $database
*/
function __construct($host="localhost",$user="root",$password="",$database="")
{
$this->set("host",$host);
$this->set("user",$user);
$this->set("password",$password);
$this->set("database",$database);
$this->connect();
}

/**
* set the value for the param of this class
*
* @param string $var
* @param string $value
*/
function set($var,$value)
{
$this->$var=$value;
}


/**
* connect to a mysql server,and choose the database.
*
* @param string $database
* @param string $host
* @param string $user
* @param string $password
* @return link_id
*/
function connect($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->user,$this->password);
if(!$this->link_id)
die("Mysql Connect Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
if(!@mysql_select_db($this->database,$this->link_id))
die("Mysql Select database Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
}
return $this->link_id;
}

/**
* query a sql into the database
*
* @param string $strsql
* @return query_id
*/
function query($strsql="")
{
if(empty($strsql)) die("Mysql Error:".__FUNCTION__."() strsql is empty!");
if($this->link_id==0) $this->connect();
if($this->debug) printf("Debug query sql:%s",$strsql);
$this->query_id=@mysql_query($strsql,$this->link_id);
if(!$this->query_id) die("Mysql query fail,Invalid sql:".$strsql.".");
return $this->query_id;
}

/**
* query a sql into the database,while it is differernt from the query() method,
* this method will return a record(array);
*
* @param string $strsql
* @param string $style
* @return $record is a array()
*/
function Execute($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,but not equal to mysql_data_seek. this methord will return a list.
*
* @param int $pos
* @param string $style
* @return record
*/
function seek($pos=0,$style="array")
{
if(!@mysql_data_seek($this->query_id,$pos))
die("Error in".__FUNCTION__."():can not seek to row ".$pos."!");
$result=@($style=="array")?mysql_fetch_array($this->query_id):mysql_fetch_object($this->query_id);
if(!$result) die("Error in ".__FUNCTION__."():can not fetch data!");
return $result;
}
/**
* free the result of query
*
*/
function free()
{
if(($this->query_id)&($this->query_id!=0))@mysql_free_result($this->query_id);
}

/**
* evaluate the result (size, width)
*
* @return num
*/
function affected_rows()
{
return @mysql_affected_rows($this->link_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 '
'.$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);
}
}
?>
推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • 在数据分析工作中,我们通常会遇到这样的问题,一个业务部门由若干业务组构成,需要筛选出每个业务组里业绩前N名的业务员。这其实是一个分组排序的 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • 本文介绍了关于apache、phpmyadmin、mysql、php、emacs、path等知识点,以及如何搭建php环境。文章提供了详细的安装步骤和所需软件列表,希望能帮助读者解决与LAMP相关的技术问题。 ... [详细]
  • 本文介绍了通过mysql命令查看mysql的安装路径的方法,提供了相应的sql语句,并希望对读者有参考价值。 ... [详细]
author-avatar
littl_eyuera
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有