首页 编程教程正文

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

piaodoo 编程教程 2020-02-02 18:11:06 1129 0 php教程

支持php4、php5的mysql数据库操作类
前端一直使用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();

   /**
    * 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 '<hr>'.$strsql.'<hr>';
   $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);
  }
}
?>

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/4228.html

评论

搜索

游戏网站源码,织梦网站源码,wordpress,wordpress主题,wordpress下载,wordpress插件,wordpress.com,wordpress模板,wordpress教程,wordpress 主题,wordpress安装,wordpress 模板,wordpress 插件,wordpress主题下载,wordpress企业主题,wordpress seo,wordpress主题开发,wordpress theme,wordpress论坛,wordpress 企业主题,wordpress主机,wordpress中文主题,wordpress cms主题,wordpress plugin,wordpress 主题下载,wordpress 主机,wordpress空间,wordpress mu,wordpress 模版,wordpress汉化主题,wordpress淘宝客主题,wordpress 空间,wordpress代码,WORDPRESS HOSTING,wordpress优点,wordpress安卓客户端,wordpress技巧,wordpress换空间,wordpress themes,网站模板,ppt模板网站,模板网站,企业网站模板,网站设计模板,免费网站模板,个人网站模板,ppt模板下载网站,网站模板下载,公司网站模板,门户网站模板,学校网站模板,网站首页模板,网站模板免费下载,旅游网站模板,网站后台模板,免费网站模板下载,传奇网站模板,网站建设模板,外贸网站模板,网站 模板,个人主页网站模板,个人网站模板下载,政府网站模板,音乐网站模板,导航网站模板,免费企业网站模板,企业网站模板下载,手表网站模板,韩国网站模板,汽车网站模板,教育网站模板,网站后台管理模板,班级网站模板,新闻网站模板,房产中介网站模板,旅游网站模板下载,工艺品网站模板,电子商务网站模板,旅游网站设计模板,团购网站模板,flash网站模板,个人网站设计模板,婚庆网站模板,广告公司网站模板,商业网站模板,手机网站模板,免费模板网站推荐,ppt免费模板网站推荐,织梦网站模板,html网站模板建站,网站html模板,免费个人网站模板,公司网站源码,sns源码,彩票网站源码,周易网站源码,源码基地,交友源码,学校网站源码,asp.net 源码,源码天下,jsp网站源码,论坛源码下载,广告联盟源码,建站源码,delphi源码,源码爱好者,酷源码,net源码,源码超市,医疗网站源码,flash源码,搜源码,源码程序,dede源码,新闻网站源码,易语言源码大全,旅游网站源码下载,flash 源码,免费源码论坛,android游戏源码,电脑维修网站源码,30源码网,股票软件源码,卖源码,源码教程,安居客 源码,vip源码,家教源码,.net源码下载,Web源码,网络公司源码,佛教网站源码,android源码学习,房产源码,钓鱼网站源码,775源码屋,web游戏源码,成品网站 源码78w78不用下载,h5游戏网站源码,asp网站源码下载,webgame源码,电子商务网站源码,vb.net源码,乐嘿源码,8a商业源码论坛,fbreader源码,在线客服系统 源码,google源码,.net网站源码,快递查询源码,源码搜藏网,dede整站源码,周易 源码,52源码论坛,财经网站源码,织梦下载站源码,qq钓鱼网站源码,flash游戏源码,房产网源码,源码搜搜,电子商务源码,团购网站源码,团购网源码,jsp源码下载,jsp源码,h站源码,8a源码,婚纱摄影网站源码,易语言盗号源码,x站源码,qq空间psd源码,免费商业源码,笑话网站源码,源码集合,源码家园,啊哦源码,星期六源码,源码熊,阿奇源码,百分百源码网,一手日源码资源,旅行网站源码,b站工程源码泄露,新站长源码,8a商业源码,asp论坛源码,flash源码下载,404源码社区,创业网站源码,php网页源码,易支付源码,成品网站w灬源码,免费CMS成品网站源码,成品网站W灬源码1688仙踪林,成品APP短视频源码下载网站,成品网站源码1688可靠吗,免费B2B网站源码,成品APP直播源码下载,国外儿童网站源码在线,成品网站W灬源码1688,源码,成品网站w灬 源码1688,免费源码网站都有哪些,成品网站源码78W78隐藏通道1,网站源码,源码网,源码网站,源码时代,源码之家,源码下载,php源码,易语言源码,源码论坛,源码是什么,商城源码,论坛源码,源码交易,源码站,源码库,免费源码,免费网站ja**源码大全,ja**源码,成品网站w灬源码1377,a5源码,站长源码,成品网站源码78W78隐藏通道1APP,源码分享,网站源码下载,源码中国,asp源码,源码社区,企业网站源码,php源码下载,成品app直播源码搭建,在线观看视频网站源码2021,旅游网站源码,安卓源码,通达信选股公式源码,神马影院php源码,c#源码,成品网站w灬源码1688网页,php 源码,网页游戏源码,android源码下载,源码吧,视频源码大全,成品短视频APP源码搭建,asp源码下载,私服源码,电脑维修源码,个人主页源码,源码出售,php网站源码,刀客源码,网址导航源码,导航网站源码,源码天空,asp 源码,软件源码,精品源码,成品网站源码1688自动跳转,个人网站源码,源码哥,在线考试系统源码,cms源码,c# 源码,商业源码,vb源码,门户网站源码,音乐网站源码,中国源码,安卓源码下载,asp网站源码,在线客服源码,电影网站源码,免费源码下载,整站源码,源码交易网,易语言源码网,.net源码,在线客服系统源码,淘客源码,卡盟源码,网站源码出售,vb源码下载,莎莎源码,熊猫烧香源码,asp.net源码,商业源码网,外贸网站源码,61源码网,zblog模板,zblog企业模板,帝国cms模板,帝国cms插件,discuz模板