一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

PDO操作MySql类分享

时间:2011-08-25 编辑:简简单单 来源:一聚教程网

为了让自己的数据类能够做到最大化的重用,就写个能够重用的PDO操作MySql的类:


由于pdo可以连接现在流行的各种数据库教程,所以单独的写个配置类类来完成不同数据库DSN的配置

php教程
/**
 * 类标准说明    PDO连接数据库的配置类
 * 类名:     ConfigDataBase
 * 功能说明:    为了让代码重用,利用此类可以动态的连接各种数据库
 * 参数说明:    $_dbms = "mysql教程";    //数据库类型
 *         $_host = '127.0.0.1';     //数据库ip地址
 *         $_port = '3306';     //数据库端口
 *         $_username = 'root';    //数据库用户名
 *        $_password = 'liujijun';   //密码
 *         $_dbname = 'zendf';        //数据库名 默认为zenf
 *         $_charset = 'utf-8';       //数据库字符编码
 *         $_dsn;//                    //data soruce name 数据源
 *
 *
 * 类属性说明:
 * 类方法说明:
 * 返回值:     不同函数返回不同的值
 * 备注说明:
 * 作者:       刘纪君
 * 最后一次修改时间:    2011下午02:01:39
 *
 */
class ConfigDataBase {
 
 protected static $_dbms = "mysql";    //数据库类型
    protected static $_host = '127.0.0.1';     //数据库ip地址
    protected static $_port = '3306';     //数据库端口
    protected static $_username = 'root';    //数据库用户名
    protected static $_password = 'liujijun';   //密码
    protected static $_dbname = 'zendf';        //数据库名 默认为zenf
    protected static $_charset = 'utf-8';       //数据库字符编码
    protected static $_dsn;//                    //data soruce name 数据源
 
 /**
  *@return   返回数据源名
  */
 public static function getDsn() {
  //将变量的值组合成  mysql:host=localhost;port =3306;dbname=test',$login,$passwd的形式
  if (!isset(self::$_dsn)){
    self::$_dsn = self::$_dbms.':host = '.self::$_host.';prot = '.
    self::$_port . ';dbname = ' . self::$_dbname.','.
    self::$_username . ','.self::$_password;
 
    if (strlen(self::$_charset) > 0){
     self::$_dsn = self::$_dsn . ';charset = ' . self::$_charset;
    }
  }
  return self::$_dsn;//返回数据源名
 }
 
 /**
  * 功能:设置$dbms
  * @param $dbms
  */
 public static function setDbms($dbms){
  if (isset($dbms) &&(strlen($dbms) > 0 )){
   self::$_dbms = trim($dbms);
  }
 }
 
 /**
  *
  * @param  $host  //数据库地址
  */
 public static function setHost($host){
  if (isset($host) &&(strlen($host) > 0 )){
   self::$_host = trim($host);
  }
 }

 /**
  *
  * @param $host 端口号
  */
 public static function setPort($port){
  if (isset($port) &&(strlen($port) > 0 )){
   self::$_post = trim($port);
  }
 }
 
 /**
  *
  * @param  $passwd 密码
  */
 public static function setPasswd($passwd){
  if (isset($passwd) &&(strlen($passwd) > 0 )){
   self::$_password = trim($passwd);
  }
 }
 
 /**
  *
  * @param  $username 用户名
  */
 public static function setUsernName($username){
   if (isset($username) &&(strlen($username) > 0 )){
    self::$_username = trim($username);
   }
  }
 
 /**
  *
  * @param  $dbname 数据库名
  */
 public static function setDbName($dbname){
   if (isset($dbname) &&(strlen($dbname) > 0 )){
    self::$_dbname = trim($dbname);
   }
  }
 
 
  /**
   *
   * @param  $charset 数据库编码
   */
 public static function setCharset($charset){
   if (isset($charset) &&(strlen($charset) > 0 )){
    self::$_charset = trim($charset);
   }
  }
}

下面是对数据库的操作:

 

require_once 'ConfigDataBase.php';
header("Content-Type: text/html; charset=utf-8");//设置编码
/**
 * 类标准说明
 * 类名:      PdoMysql
 * 功能说明:     对数据库进行各种操作
 * 参数说明:
 * 类属性说明:
 * 类方法说明:
 * 返回值:
 * 备注说明:
 * 作者:       刘纪君
 * 最后一次修改时间:    2011上午10:45:36
 *
 */
class  PdoMysqlOperater{
 
 
 /**
  * @return 返回连接数据库的句柄
  */
 public function getConnection(){
  $connection = NULL;
  try {
   $connection = new PDO(ConfigDataBase::getDsn());
   echo 'Success';
  } catch (PDOException  $e) {
   print "Error in connection :".$e->getMessage().' '.die();
  }
  return $connection;
 }
 
 /**
  *
  * @param  $connection    连接数据库的句柄
  */
 public function closeConnection($connection){
  try {
   if ($connection != null) {
    $connection = null;//关闭数据库连接句柄
   }
  } catch (Exception $e) {
   print 'Close the connectin is error:'.$e->getMessage();
  }
 
 }
 
 /**
  * 功能:      向数据库中增加数据
  * @param $sql      sql语句
  */
 public  function insertDatabase($sql){
  $affect = false;//失败返回false
  try {
   $conn = $this->getConnection();
   $conn->exec($sql);
   $affect = true;//插入成功返回true
   $this->closeConnection($conn);//关闭数据库
  } catch (PDOException $e) {
   print 'Insert error '.$e->getMessage();
  }
  return $affect;//返回值
 }
 
 /**
  *
  * @param $id      表的主键id
  * @param $tableName    表名
  */
 public function deleltById($id,$tableName){
  $affact = false;
  $sql = 'delete from '.trim($tableName).' where id = '.$id;
  try {
   $conn = $this->getConnection();
   $conn->exec($sql);
   $this->closeConnection($conn);
   $affact = true;
  } catch (PDOException  $e) {
   print 'Delelte error is '.$e->getMessage();
  }
  return $affact;
 }
 
 /**
  * 功能:      以and 的形式删除记录
  * @param $tableName    表的名称
  * @param $array        数组表中字段名=其值的方式进行组合
  */
 public  function prepareDeleteAnd($tableName,array $array=null){
  $sql = 'delete from '. $tableName . ' where ';
  $count = count($array);//计算数组的长度
  $flag = 0;//设置标记

  foreach ($array as $key => $value){
   $flag++;//让flag增加一
   $sql .= $key .'='."'".$value."'";
   if ($flag != $count ){//当falg不等于count时,数组还有值,后面增加and,反之不增加
    $sql .= ' and ';
   }
  }
  echo  $sql;//测试sql语句的组合 
  try {
   $conn = $this->getConnection();//获取连接
   $conn->prepare($sql);
   $this->closeConnection();
  } catch (PDOException $e) {
   print 'Delete error is '.$e->getMessage();
  }
 
 }
 

 /**
  * 功能:         以or 的形式删除记录
  * @param $tableName    表的名称
  * @param $array        数组表中字段名=其值的方式进行组合
  */
 public  function prepareDeleteOr($tableName,array $array=null){
 
  $sql = 'delete from '. $tableName . ' where ';
  $count = count($array);//计算数组的长度
  $flag = 0;//设置标记

  foreach ($array as $key => $value){
   $flag++;//让flag增加一
   $sql .= $key .'='."'".$value."'";
   if ($flag != $count ){//当falg不等于count时,数组还有值,后面增加and,反之不增加
    $sql .= ' or ';
   }
  }
  echo  $sql;//测试sql语句的组合 
  try {
   $conn = $this->getConnection();//获取连接
   $stmt = $conn->prepare($sql);
   $stmt->execute();//执行
   $this->closeConnection();
  } catch (PDOException $e) {
   print 'Delete error is '.$e->getMessage();
  }
 
 }
 
 
 /**
  * 功能:      取得表中所有数据
  * @param  $sql     sql语句
  */
 public function getAll($sql){
 
  $result = null;
  try {
   $conn = $this->getConnection();
   $result = $conn->query($sql);
   $this->closeConnection($conn);
  } catch (PDOException $e) {
   print 'GetAll error is '.$e->getMessage();
  }
 }
 
 
 /**
  * 功能:更新数据表中的信息
  * @param  $table      要更新的表名
  * @param array $updateFiled    要更新的字段
  * @param array $updateConditon 更新需要的条件
  */
 public function updateDataBase($table,array $updateFiled,array $updateConditon ){
  
  $sql   = 'update from ' .$table .' set ';
 
  //对set字段进行赋值操作
  $count = count($updateFiled);//获取要修改数组的长度
  $flag  = 0;//设置标记为0
  foreach ($updateFiled as $key => $value){
   $flag++;
   $sql .= $key .'='."'".$value."'";
   if ($flag != $count){
    $sql .=',';
   }
  }
  //对where条件进行赋值
  $countUpdateCondition = count($updateConditon);//获取要修改数组的长度
  $flag  = 0;//设置标记为0
  $sql .= ' where ';
  foreach ($updateConditon as $key => $value){
   $flag++;
   $sql .= $key .'='."'".$value."'";
   if ($flag != $countUpdateCondition){
    $sql .=' and ';
   }
  }
  try {
   $conn = $this->getConnection();
   $conn->exec($sql);
   $this->closeConnection($conn);
  } catch (PDOException $e) {
   print 'Update error is :'.$e->getMessage();
  }
 
 }
 
 
 /**
  * 功能:      根据表和提高的查询条件进行查询
  * 返回值:      返回结果集
  * @param  $table    数据表名
  * @param array $findCondition  查询条件
  */
 public function findData($table,array $findCondition){
 
  $sql = 'select from '.$table .' where ';
 
  $count = count($findCondition);//获取查询条件数组的长度
  $flag  = 0;//设置标记为0
  foreach ($findCondition as $key => $value){
   $flag++;
   $sql .= $key .'='."'".$value."'";
   if ($flag != $count){
    $sql .=' and ';
   }
  }
  try {
    $conn = $this->getConnection();
    $conn->exec($sql);
    $this->closeConnection($conn);
   } catch (PDOException $e) {
    print 'find error is :'.$e->getMessage();
   }
  
  }
}
//测试

$db = new PdoMysqlOperater();
$db->findData('liujijun',array('name'=>'liujijun','name1'=>'liujijun'));

热门栏目