php链接mysql数据库类

clsMysql.php

<?php
/*
*PHP连接数据库类
*属性:主机名($myDbhost)、用户名($myDbuser)、密码($myDbwd)、数据库名($myDbname)、编码($myCode)、连接($myPconn)、sql($mySql)、连接标识($linkId)
*/
class clsMysql
{
 protected $myDbhost='';//数据库主机名
 protected $myDbuser='';//数据库用户名
 protected $myDbwd='';//数据库用户密码
 protected $myDbname='';//数据库名
 protected $myCode='';//数据库执行编码
 protected $myPconn=0;//默认0为临时,1为持久
 protected $mySql='';
 protected $linkId='';
 protected $errorPath='';
 protected $errorOpen=0;//0表示关闭错误信息,1表示打开错误信息
 /*构造函数
 *参数:主机名、用户名、密码、数据库名、编码、连接
 */
 public function __construct($myDbhost,$myDbuser,$myDbwd,$myDbname,$errorOpen=0,$errorPath='error',$myCode='gb2312',$myPconn=0)
 {
  //初始化属性
  // error_reporting(0);
   $this -> myDbhost=$myDbhost;
   $this -> myDbuser=$myDbuser;
   $this -> myDbwd=$myDbwd;
   $this -> myDbname=$myDbname;
   $this -> myCode=$myCode;
   $this -> myPconn=$myPconn;
   $this -> errorPath=$errorPath;
   $this -> errorOpen=$errorOpen;
  }
  //连接数据库
  public function openDbconn()
  {
   //判断连接
   if($this -> myPconn)
   {
    $this -> linkId = mysql_pconnect($this -> myDbhost,$this -> myDbuser,$this -> myDbwd); 
   }
   else
   {
    $this -> linkId = mysql_connect($this -> myDbhost,$this -> myDbuser,$this -> myDbwd); 
   } 
   if(!$this -> linkId)
   {
    if($this -> errorOpen)
    $this -> errProc($this -> mysqlNo(),$this -> mysqlError(),$_SERVER['SCRIPT_NAME'],$this -> sql);
    return false  ;
   }
  }
  //选择数据
  public function selectedDb()
  {
    $cn=mysql_select_db($this -> myDbname) ;
    if(!$cn)
    {
     if($this -> errorOpen)
     $this -> errProc($this -> mysqlNo(),$this -> mysqlError(),$_SERVER['SCRIPT_NAME'],$this -> sql);
     return false;   
    }
  }
  //转换编码
  public function changeCode()
  {
   mysql_query("set names ".$this -> myCode) ;
  }
  /*读取数据
  *参数:结果集
  */
  public function fetchArray($rs)
  {
    mysql_fetch_array($rs) ;
  }
  //影响的行数
  public function affectedRows()
  {
    return mysql_affected_rows($this -> linkId); 
  }
  //得到错误的信息
  public function mysqlError()
  {
    return mysql_error();
  }
  //得到错误的编号
  public function mysqlNo()
  {
       return mysql_errno();
  }
  /*执行sql语句
  *参数:$sql
  */
  public function query($sql)
  {
   $this -> sql=$sql;
   $this -> openDbconn();
   $this -> selectedDb();
   $this -> changeCode();
   $this -> affectedRows();
   $rs=mysql_query($this -> sql);
   if($rs)
   {
    return $rs; 
   }
   else
   {
    if($this -> errorOpen)
    $this -> errProc($this -> mysqlNo(),$this -> mysqlError(),$_SERVER['SCRIPT_NAME'],$this -> sql);
    return false; 
   }
  }
  /*组合SQL语句
  *参数:表名(table),字段(arrayFiled),方式(mode),条件(where)
  */
  public function autoExecute($table,$arrayFiled,$mode,$where="")
  {
   //判断表是否存在
   if(!$table)
   {
    return false; 
   }
   if(strtolower($mode)=="insert")
   {
        //组合增加SQL语句
     foreach($arrayFiled as $k => $v)
     {
       $filed[]=$k;
    $value[]=$v;
     }
     $filed=implode(",",$filed);
     $value=implode(",",$value);
     $sql="insert into ".$table."(".$filed.")values(".$value.")";
    // echo $sql;
     //exit;
     $this ->sql=$sql;
   }
   if(strtolower($mode)=="update")
   {
     //组合修改SQL语句
     foreach($arrayFiled as $k => $v)
     {
      $filed[]=$k."=".$v;  
     }
     $filed=implode(",",$filed);
     if(!where)
     {
      return false;  
      }
   $sql="update ".$table." set ".$filed." ".$where."";
   //echo $sql;
   //exit;
   $this ->sql=$sql;
   }
   if($this -> sql)
   {
      $this ->query($this ->sql);
   }
   else
   {
     return false; 
   }  
  }
  //查询
  public function getFetch($sql)
  {
   if(!$sql)
   {
     return false; 
    }
    $this -> sql=$sql;
    //echo $this -> sql;
    $rs=$this ->query($this -> sql);
    while($arr=mysql_fetch_array($rs))
    {
    $a[]=$arr;
  }
  if($a)
  {
   return $a; 
     }
  else
  {
     return false; 
     }
   }
   /*错误日志
   *参数:错误的编号,错误的信息,错误的文件,错误的SQL语句
   */
   private function errProc($eNo,$eMsg,$eFile,$eSql)
   {
    //判断用户给定的文件夹是否存在
    $ePath=dirname(__FILE__).$this -> errorPath."//";
    $ePath=str_replace("include","",$ePath);
    //echo $ePath;
    if(!file_exists($ePath))
    {
     mkdir($ePath);
  }
  //根据当前时间生成文件夹
     if(!file_exists($ePath.date("Y_m_d")))
  {
       mkdir($ePath.date("Y_m_d")); 
     }
  //在当前的目录下生成错误日志
    $msg="********错误开始********"."/n";
    $msg.="错误的编号:".$eNo."/n";
    $msg.="错误的信息:".$eMsg."/n";
    $msg.="错误的文件:".$eFile."/n";
    $msg.="错误的SQL:".$eSql."/n";
    $msg.="错误的时间:".date("Y-m-d H-i-s",time()+8*3600)."/n";
    $msg.="********错误结束********"."/n";
    error_log($msg,3,$ePath.date("Y_m_d")."//"."error.err");
   }
}
?>

 

init.php

<?php
//定义配置文件
   define("HOSTNAME",'localhost');
   define("DBUSER",'root');
   define("DBPWD",'123');
   define("DBNAME",'date');
   define("CODE",'GB2312');
   define("PCONN",0);
   define("ERRORPATH",'error');
   define("ERROROPEN",1);//0表示关闭错误信息,1表示打开错误信息
   ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值