PHP视频教学,让你更快更轻松的掌握PHP。

查看完整版本: 一个数据库类(自创)

离群的猴子 2007-9-27 10:57

一个数据库类(自创)

[code]<?php
   //对MySQL数据库进行操作的类
   class mySQL_Class
   {
         //变量(属性)声明
   var $conn;          //链接对象
   private $Server;    //服务器
   private $Uid;       //用户名
   private $Pwd;       //密码
   private $DataBase;  //数据库
   //*********************************************//
   //***   mySQL_Class类的构造函数             ***//
   //***   参数$server  : mysql服务器          ***//
   //***   参数$uid     : mysql服务器用户      ***//
   //***   参数$pwd     : mysql服务器密码      ***//
   //***   参数$dataBase: 要链接数据库         ***//
   //*********************************************//
   function mySQL_Class($server="localhost",$uid="root",$pwd="chaizhiyong",$dataBase="myData")
   {
      $this->Server=$server;
      $this->Uid=$uid;
      $this->Pwd=$pwd;
      $this->DataBase=$dataBase;
   $this->conn=@mysql_connect($this->Server,$this->Uid,$this->Pwd) or die("链接服务器失败,程序中止!");
   @mysql_select_db($this->DataBase,$this->conn) or die("选择数据库失败,程序中止!");
   @mysql_query("set names 'gbk'",$this->conn);
   }
   //*********************************************//
   //***   mySQL_Class类的SelectDB函数         ***//
   //***   功能: 利用$this->conn中的链接对象, ***//
   //***         选择数据库,并设置字符集。    ***//
   //***  -----------------------------------  ***//
   //***   参数$dataBase: 要链接数据库         ***//
   //*********************************************//
   function SelectDB($dataBase)
   {
       @mysql_select_db($dataBase,$this->conn) or die("选择数据库失败,程序中止!");
     @mysql_query("set names 'gbk'",$this->conn);
   }
   //*********************************************//
   //***   mySQL_Class类的QueryRS函数          ***//
   //***   功能: 利用$this->conn中的链接对象, ***//
   //***         执行select语句,并返回记录集。***//
   //***  -----------------------------------  ***//
   //***   参数$sql: 要执行的select语句        ***//
   //*********************************************//
   function QueryRS($sql)
   {
      conn">$rs=@mysql_query($sql,$this->conn);
   return $rs;
   }

   //*********************************************//
   //***   mySQL_Class类的QueryRow函数         ***//
   //***   功能: 利用$this->conn中的链接对象, ***//
   //***         执行select语句,并把第一条记录***//
   //***         以数组的方式进行返回。        ***//
   //***  -----------------------------------  ***//
   //***   参数$sql: 要执行的select语句        ***//
   //*********************************************//
   function QueryRow($sql)
   {
      conn">$rs=@mysql_query($sql,$this->conn);
   if($rs)
   {
       $arr=@mysql_fetch_array($rs);
    return $arr;
   }else{
     return NULL;
   }
   }
   //*********************************************//
   //***   mySQL_Class类的QueryOne函数         ***//
   //***   功能: 利用$this->conn中的链接对象, ***//
   //***         执行select语句,并返回第一条  ***//
   //***         记录第一个字段的值。          ***//
   //***  -----------------------------------  ***//
   //***   参数$sql: 要执行的select语句        ***//
   //*********************************************//
   function QueryOne($sql)
   {
      conn">$rs=@mysql_query($sql,$this->conn);
   if($rs)
   {
       $arr=@mysql_fetch_array($rs);
    return $arr[0];
   }else{
     return NULL;
   }
   }
   //*********************************************//
   //***   mySQL_Class类的QuerySQL函数         ***//
   //***   功能: 利用$this->conn中的链接对象, ***//
   //***         执行insert、update、delete语句***//
   //***         并返回语句执行后受影响的记录  ***//
   //***         行数。                        ***//
   //***  -----------------------------------  ***//
   //***   参数$sql: 要执行的sql语句        ***//
   //*********************************************//
   function QuerySQL($sql)
   {
      conn">$rs=@mysql_query($sql,$this->conn);
   if($rs)
   {
       conn">$num=@mysql_affected_rows($this->conn);
    return $num;
   }else{
       return 0;
   }
   }
  }
?>[/code]

robin 2007-9-27 12:18

不错 收藏先

淡水河边 2007-10-13 10:15

猴子NB.顶

离群的猴子 2007-10-14 11:44

你们才是N人哈!我们一般哈..................

lipeng8821 2008-4-22 15:36

:69:

gjgj 2008-5-8 14:33

谢谢,顶你。。:23:

mhb0000 2008-6-18 17:01

好--------顶你---------
(新人-请多多关照!!)

zxcvva 2008-8-27 11:08

嗯,非常不错。。收藏。。:37: :37:

lxylxy8866 2008-8-27 18:21

:44:

lampbrothers 2008-9-18 11:54

好东西 支持下~~:13:
页: [1]
查看完整版本: 一个数据库类(自创)