php封装的pdo数据库操作工具类与用法示例_php技巧_脚本之家

金沙网址,php封装的pdo数据库操作工具类与用法示例_php技巧_脚本之家。正文实例汇报了php封装的pdo数据库操作工具类与用法。共享给我们供我们仿效,具体如下:

 'localhost', 'username' => 'root', 'password' => '1234', 'database' => 'test', 'hostport' => '3306', 'dbms' => 'mysql', 'dsn' => 'mysql:host=localhost;dbname=test' ); } if(empty($dbConfig['hostname'])){ self::throw_exception; } self::$config = $dbConfig; if(empty(self::$config['params'])){ self::$config['params'] = array(); } if{ $configs = self::$config; if{ //开启长连接,添加到配置数组中 $configs['params'][constant("PDO::ATTR_PERSISTENT")] = true; } try { self::$link = new PDO($configs['dsn'],$configs['username'],$configs['password'],$configs['params']); } catch  { self::throw_exception; } if{ self::throw_exception; return false; } self::$link->exec; self::$dbVersion = self::$link->getAttribute(constant("PDO::ATTR_SERVER_VERSION")); unset; } } /** * 得到所有记录 * * @param  $sql The sql * * @return  All. */ public static function getAll{ if{ self::query; } $result = self::$PDOStatement->fetchAll(constant; return $result; } /** * 得到一条记录 * * @param  $sql The sql * * @return  The row. */ public static function getRow{ if{ self::query; } $result = self::$PDOStatement->fetch(constant; return $result; } /** * 执行增删改操作,返回受影响记录的条数 * * @param  $sql The sql * * @return boolean ( description_of_the_return_value ) */ public static function execute{ $link = self::$link; ifreturn false; if{ self::$queryStr = $sql; } if(!emptyself::free(); $result = $link->exec; self::haveErrorThrowException{ self::$lastInsertId = $link->lastInsertId(); self::$numRows = $result; return $result; }else{ return false; } } /** * 根据主键查找记录 * * @param  $tabName The tab name * @param  $priId The pri identifier * @param string $fields The fields * * @return  ( description_of_the_return_value ) */ public static function findById($tabName,$priId,$fields='*'){ $sql = 'SELECT %s FROM %s WHERE id=%d'; return self::getRow(sprintf($sql,self::parseFields,$tabName,$priId)); } /** * 执行普通查询 * * @param  $tables The tables * @param  $where The where * @param string $fields The fields * @param  $group The group * @param  $having The having * @param  $order The order * @param  $limit The limit * * @return  ( description_of_the_return_value ) */ public static function find($tables,$where=null,$fields='*',$group=null,$having=null,$order=null,$limit =null){ $sql = 'SELECT '.self::parseFields.' FROM '.$tables .self::parseWhere .self::parseGroup .self::parseHaving .self::parseOrder .self::parseLimit; $data = self::getAll; return $data; } /** * 添加记录 * * @param  $data The data * @param  $table The table * * @return  ( description_of_the_return_value ) */ public static function add{ $keys = array_keys; array_walk($keys, array('PdoMySQL','addSpecialChar')); $fieldsStr = join; $values = "'".join("','",array_values."'"; $sql = "INSERT {$table} VALUES"; return self::execute; } /** * 更新数据 * * @param  $data The data * @param  $table The table * @param  $where The where * @param  $order The order * @param  $limit The limit */ public static function update($data,$table,$where=null,$order=null,$limit=null){ $sets = ''; foreach ($data as $key => $value) { $sets .= $key."='".$value."',"; } $sets = rtrim; $sql = "UPDATE {$table} SET {$sets}".self::parseWhere.self::parseOrder.self::parseLimit; echo $sql; } /** * 删除数据 * * @param  $data The data * @param  $table The table * @param  $where The where * @param  $order The order * @param  $limit The limit * * @return  ( description_of_the_return_value ) */ public static function delete($table,$where=null,$order=null,$limit=null){ $sql = "DELETE FROM {$table} ".self::parseWhere.self::parseOrder.self::parseLimit; return self::execute; } /** * 执行查询 * * @param string $sql The sql * * @return boolean ( description_of_the_return_value ) */ public static function query{ $link = self::$link; ifreturn false; //判断之前是否有结果集,如果有的话,释放结果集 if(!emptyself::free(); self::$queryStr = $sql; self::$PDOStatement = $link->prepare; $res = self::$PDOStatement->execute(); self::haveErrorThrowException(); return $res; } /** * 获取最后执行的sql * * @return boolean The last sql. */ public static function getLastSql(){ $link = self::$link; if{ return false; } return self::$queryStr; } /** * 获取最后插入的ID * * @return boolean The last insert identifier. */ public static function getLastInsertId(){ $link = self::$link; if{ return false; } return self::$lastInsertId; } /** * 获得数据库的版本 * * @return boolean The database version. */ public static function getDbVersion(){ $link = self::$link; if{ return false; } return self::$dbVersion; } /** * 得到数据库中表 * * @return array ( description_of_the_return_value ) */ public static function showTables; if(self::query{ $result = self::getAll(); foreach ($result as $key => $value) { $tables[$key] = current; } } return $tables; } /** * 解析where条件 * * @param  $where The where * * @return  ( description_of_the_return_value ) */ public static function parseWhere{ $whereStr = ''; if&&!empty{ $whereStr = $where; } return empty ? '' : ' WHERE '.$whereStr; } /** * 解析group * * @param  $group The group * * @return  ( description_of_the_return_value ) */ public static function parseGroup{ $groupStr = ''; if{ $groupStr = implode; }elseif&&!empty{ $groupStr = $group; } return empty ? '' : ' GROUP BY '.$groupStr; } /** * 解析having * * @param  $having The having * * @return  ( description_of_the_return_value ) */ public static function parseHaving{ $havingStr = ''; if&&!empty{ $havingStr = $having; } return empty ? '' : ' HAVING '.$havingStr; } /** * 解析order * * @param  $order The order * * @return  ( description_of_the_return_value ) */ public static function parseOrder{ $orderStr = ''; if&&!empty{ $orderStr = $order; } return empty ? '' : ' ORDER BY '.$orderStr; } /** * 解析limit * * @param  $limit The limit * * @return  ( description_of_the_return_value ) */ public static function parseLimit{ $limitStr = ''; if{ $limitStr = implode; }elseif&&!empty{ $limitStr = $limit; } return empty ? '' : ' LIMIT '.$limitStr; } /** * 解析字段 * * @param  $fields The fields * * @return string ( description_of_the_return_value ) */ public static function parseFields{ if{ array_walk($fields, array('PdoMySQL','addSpecialChar')); $fieldsStr = implode; }elseif &&! { if===false){ $fields = explode; array_walk($fields, array('PdoMySQL','addSpecialChar')); $fieldsStr = implode; }else{ $fieldsStr = $fields; } }else{ $fieldsStr = "*"; } return $fieldsStr; } /** * 通过反引号引用字字段 * * @param string $value The value * * @return string ( description_of_the_return_value ) */ public static function addSpecialChar{ if($value==="*"||strpos!==false||strpos{ //不用做处理 }elseif===false){ $value = '`'.trim.'`'; } return $value; } /** * 释放结果集 */ public static function free(){ self::$PDOStatement = null; } /** * 抛出错误信息 * * @return boolean ( description_of_the_return_value ) */ public static function haveErrorThrowException(){ $obj = empty ? self::$link : self::$PDOStatement; $arrError = $obj->errorInfo(); if($arrError[0]!='00000'){ self::$error = 'SQLSTATE=>'.$arrError[0].'
SQL Error=>'.$arrError[2].'
Error SQL=>'.self::$queryStr; self::throw_exception; return false; } if{ self::throw_exception; return false; } } /** * 自定义错误处理 * * @param  $errMsg The error message */ public static function throw_exception{ echo $errMsg; } /** * 销毁连接对象,关闭数据库 */ public static function close(){ self::$link = null; }}$pdo = new PdoMysql();var_dump;

越来越多关于PHP相关内容感兴趣的读者可查阅本站专项论题:《PHP基于pdo操作数据库能力总括》、《php+mysqli数据库程序设计手艺总括》、《php面向对象程序设计入门教程》、《php字符串用法总计》、《php+mysql数据库操作入门教程》及《php屡见不鲜数据库操作能力汇总》

但愿本文所述对大家PHP程序设计有着帮忙。

发表评论

电子邮件地址不会被公开。 必填项已用*标注