首页 > 作文

php封装的pdo数据库操作工具类与用法示例

更新时间:2023-04-07 08:54:38 阅读: 评论:0

本文实例讲述了php封装的pdo数据库操作工具类与用法。分享给大家供大家参考,具体如下:

<?phpheader("content-type:text/html;chart=utf-8");class pdomysql{public static $config = array();//设置连接参数,配置信息public static $link = null;//保存连接标识符public static $pconnect = fal;//是否开启长连接public static $dbversion = null;//保存数据库版本public static $connected = fal;//判断是否连接成功public static $pdostatement = null;//保证pdostatement对象public static $querystr = null;//保存最后执行的操作public static $error = null;//保存错误信息public static $lastinrtid = null;//保存上一步插入操作保存的auto_incremantpublic static $numrows = null;//受影响记录的条数/*** 构造函数,连接数据库** @param   array|string $dbconfig the databa configuration** @return   boolean    ( description_of_the_return_value )*/public function __construct($dbconfig=''){if(!class_exists("pdo")){lf::throw_exception("不支持pdo,请先开启");}if(!is_array($dbconfig)){$dbconfig = array('hostname' => 'localhost','urname' => 'root','password' => '1234','databa' => 'test','hostport' => '3306','dbms'   => 'mysql','dsn'   => 'mysql:host=localhost;dbname=test');}if(empty($dbconfig['hostname'])){lf::throw_exception("没有定义数据库配置,请先定义");}lf::$config = $dbconfig;if(empty(lf::$config['params'])){lf::$config['params'] = array();}if(!ist(lf::$link)){$configs = lf::$config;if(lf::$pconnect){//表白的短句开启长连接,添加到配置数组中$configs['params'][constant("pdo::attr_persistent")] = true;}try {lf::$link = new pdo($configs['dsn'],$configs['urname'],$configs['password'],$configs['params']);} catch (pdoexception $e) {lf::throw_exception($e->getmessage());}if(!lf::$link){lf::throw_ex谷雨图片大全ception("pdo连接错误");return fal;}lf::$link->exec("t names utf8");lf::$dbversion = lf::$link->getattribute(constant("pdo::attr_rver_version"));unt($configs);}}/*** 得到所有记录** @param   <type> $sql  the sql** @return   <type> all.*/public static function getall($sql=null){if($sql!=null){lf::query($sql);}$result = lf::$pdostatement->fetchall(constant("pdo::fetch_assoc"));return $result;}/*** 得到一条记录** @param   <type> $sql  the sql** @return   <type> the row.*/public static function getrow($sql=null){if($sql!=null){lf::query($sql);}$result = lf::$pdostatement->fetch(constant("pdo::fetch_assoc"));return $result;}/*** 执行增删改操作,返回受影响记录的条数** @param   <type>  $sql  the sql** @return   boolean ( description_of_the_return_value )*/public static function execute($sql=null){$link = lf::$link;if(!$link)return fal;if($sql!=null){lf::$querystr = $sql;}if(!empty(lf::$pdostatement))lf::free();$result = $link->exec(lf::$querystr);lf::haveerrorthrowexception();if($result){lf::$lastinrtid = $link->lastinrtid();lf::$numrows = $result;return $result;}el{return fal;}}/*** 根据主键查找记录** @param   <type> $tabname the ta和女生聊天话题b name* @param   <type> $priid  the pri identifier* @param   string $fields  the fields** @return   <type> ( description_of_the_return_value )*/public static function findbyid($tabname,$priid,$fields='*'){$sql = 'lect %s from %s where id=%d';return lf::getrow(sprintf($sql,lf::parfields($fields),$tabname,$priid));}/*** 执行普通查询** @param   <type> $tables the tables* @param   <type> $where  the where* @param   string $fields the fields* @param   <type> $group  the group* @param   <type> $having the having* @param   <type> $order  the order* @param   <type> $limit  the limit** @return   <type> ( description_of_the_return_value )*/public static function find($tables,$where=null,$fields='*',$group=null,$having=null,$order=null,$limit=null){$sql = 'lect '.lf::parfields($fields).' from '.$tables.lf::parwhere($where).lf::pargroup($group).lf::parhaving($having).lf::parorder($order).lf::parlimit($limit);$data = lf::getall($sql);return $data;}/*** 添加记录** @param   <type> $data  the data* @param   <type> $table the table** @return   <type> ( description_of_the_return_value )*/public static function add($data,$table){$keys = array_keys($data);array_walk($keys, array('pdomysql','addspecialchar'));$fieldsstr = join(',',$keys);$values = "'".join("','",array_values($data))."'";$sql = "inrt {$table}({$fieldsstr}) values({$values})";return lf::execute($sql);}/*** 更新数据** @param   <type> $data  the data* @param   <type> $table the table* @param   <type> $where the where* @param   <type> $order the order* @param   <type> $limit the limit*/public static function update($data,$table,$where=null,$order=null,$limit=null){$ts = '';foreach ($data as $key => $value) {$ts .= $key."='".$value."',";}$ts = rtrim($ts,',');$sql = "update {$table} t {$ts}".lf::parwhere($where).lf::parorder($order).lf::parlimit($limit);echo $sql;}/*** 删除数据** @param   <type> $data  the data* @param   <type> $table the table* @param   <type> $where the where* @param   <type> $order the order* @param   <type> $limit the limit** @return   <type> ( description_of_the_return_value )*/public static function delete($table,$where=null,$order=null,$limit=null){$sql = "delete from {$table} ".lf::parwhere($where).lf::parorder($order).lf::parlimit($limit);return lf::execute($sql);}/*** 执行查询** @param   string  $sql  the sql** @return   boolean ( description_of_the_return_value )*/public static function query($sql=''){$link = lf::$link;if(!$link)return fal;//判断之前是否有结果集,如果有的话,释放结果集if(!empty(lf::$pdostatement))lf::free();lf::$querystr = $sql;lf::$pdostatement = $link->prepare(lf::$querystr);$res = lf::$pdostatement->execute();lf::haveerrorthrowexception();return $res;}/*** 获取最后执行的sql** @return   boolean the last sql.*/public static function getlastsql(){$link = lf::$link;if(!$link){return fal;}return lf::$querystr;}/*** 获取最后插入的id** @return   boolean the last inrt identifier.*/public static function getlastinrtid(){$link = lf::$link;if(!$link){return fal;}return lf::$lastinrtid;}/*** 获得数据库的版本** @return   boolean the databa version.*/public static function getdbversion(){$link = lf::$link;if(!$link){return fal;}return lf::$dbversion;}/*** 得到数据库中表** @return   array ( description_of_the_return_value )*/public static function showtables(){$tables = array();if(lf::query("show tables")){$result =重庆二本大学排名榜 lf::getall();foreach ($result as $key => $value) {$tables[$key] = current($value);}}return $tables;}/*** 解析where条件** @param   <type> $where the where** @return   <type> ( description_of_the_return_value )*/public static function parwhere($where){$wherestr = '';if(is_string($where)&&!empty($where)){$wherestr = $where;}return empty($wherestr) ? '' : ' where '.$wherestr;}/*** 解析group** @param   <type> $group the group** @return   <type> ( description_of_the_return_value )*/public static function pargroup($group){$groupstr = '';if(is_array($group)){$groupstr = implode(',', $group);}elif(is_string($group)&&!empty($group)){$groupstr = $group;}return empty($groupstr) ? '' : ' group by '.$groupstr;}/*** 解析having** @param   <type> $having the having** @return   <type> ( description_of_the_return_value )*/public static function parhaving($having){$havingstr = '';if(is_string($having)&&!empty($having)){$havingstr = $having;}return empty($havingstr) ? '' : ' having '.$havingstr;}/*** 解析order** @param   <type> $order the order** @return   <type> ( description_of_the_return_value )*/public static function parorder($order){$orderstr = '';if(is_string($order)&&!empty($order)){$orderstr = $order;}return empty($orderstr) ? '' : ' order by '.$orderstr;}/*** 解析limit** @param   <type> $limit the limit** @return   <type> ( description_of_the_return_value )*/public static function parlimit($limit){$limitstr = '';if(is_array($limit)){$limitstr = implode(',', $limit);}elif(is_string($limit)&&!empty($limit)){$limitstr = $limit;}return empty($limitstr) ? '' : ' limit '.$limitstr;}/*** 解析字段** @param   <type> $fields the fields** @return   string ( description_of_the_return_value )*/public static function parfields($fields){if(is_array($fields)){array_walk($fields, array('pdomysql','addspecialchar'));$fieldsstr = implode(',', $fields);}elif (is_string($fields)&&!(empty($fields))) {if(strpos($fields, '`')===fal){$fields = explode(',', $fields);array_walk($fields, array('pdomysql','addspecialchar'));$fieldsstr = implode(',', $fields);}el{$fieldsstr = $fields;}}el{$fieldsstr = "*";}return $fieldsstr; }/*** 通过反引号引用字字段** @param   string $value the value** @return   string ( description_of_the_return_value )*/public static function addspecialchar(&$value){if($value==="*"||strpos($value,'.')!==fal||strpos($value,'`')!==fal){//不用做处理}elif(strpos($value, '`')===fal){$value = '`'.trim($value).'`';}return $value;}/*** 释放结果集*/public static function free(){lf::$pdostatement = null;}/*** 抛出错误信息** @return   boolean ( description_of_the_return_value )*/public static function haveerrorthrowexception(){$obj = empty(lf::$pdostatement) ? lf::$link : lf::$pdostatement;$arrerror = $obj->errorinfo();if($arrerror[0]!='00000'){lf::$error = 'sqlstate=>'.$arrerror[0].'<br/>sql error=>'.$arrerror[2].'<br/>error sql=>'.lf::$querystr;lf::throw_exception(lf::$error);return fal;}if(lf::$querystr==''){lf::throw_exception('没有执行sql语句');return fal;}}/*** 自定义错误处理** @param   <type> $errmsg the error message*/public static function throw_exception($errmsg){提纯echo $errmsg;}/*** 销毁连接对象,关闭数据库*/public static function clo(){lf::$link = null;}}$pdo = new pdomysql();var_dump($pdo->showtables());

更多关于php相关内容感兴趣的读者可查看本站专题:《php基于pdo操作数据库技巧总结》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家php程序设计有所帮助。

本文发布于:2023-04-07 08:54:36,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/d88d8080a9eaff6ab5875788e98401f4.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:php封装的pdo数据库操作工具类与用法示例.doc

本文 PDF 下载地址:php封装的pdo数据库操作工具类与用法示例.pdf

标签:数据库   操作   程序设计   请先
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图