针对thinkPHP5框架存储过程bug重写的存储过程扩展类完整实例

本文实例讲述了针对thinkPHP5框架存储过程bug重写的存储过程扩展类。分享给大家供大家参考,具体如下:

近期用tp5框架调取存储过程发现有bug,借鉴了一些官方的函数、以及找了个MysqLi的类把存储过程重新写了个扩展类,下面两个类直接放置项目extend目录的stored(这个文件名称请按个人习惯命名)目录下,需要注意的是类增加命名空间namespace stored就OK。

1、MysqLi类,此类直接在网上找的,新增一个getAllData()函数获取存储过程多条数据集

rush:PHP;"> host = $config['hostname'] ? $config['hostname'] : 'localhost'; $this->port = $config['hostport'] ? $config['hostport'] : '3306'; $this->user = $config['username'] ? $config['username'] : 'root'; $this->pass = $config['password'] ? $config['password'] : 'root'; $this->db = $config['database'] ? $config['database'] : 'zhijian'; $this->charset=isset($config['charset']) ? $config['charset'] : 'utf8'; //连接数据库 $this->db_connect(); //选择数据库 $this->db_usedb(); //设置字符集 $this->db_charset(); } //连接数据库 private function db_connect(){ $this->link=MysqLi_connect($this->host,$this->user,$this->pass); if(!$this->link){ echo "数据库连接失败
"; echo "错误编码".MysqLi_errno($this->link)."
"; echo "错误信息".MysqLi_error($this->link)."
"; exit; } } //设置字符集 private function db_charset(){ MysqLi_query($this->link,"set names {$this->charset}"); } //选择数据库 private function db_usedb(){ MysqLi_query($this->link,"use {$this->db}"); } //私有的克隆 private function __clone(){ die('clone is not allowed'); } //公用的静态方法 public static function getIntance(){ if(self::$dbcon==false){ self::$dbcon=new self; } return self::$dbcon; } //执行sql语句的方法 public function query($sql){ $res=MysqLi_query($this->link,$sql); if(!$res){ echo "sql语句执行失败
"; echo "错误编码是".MysqLi_errno($this->link)."
"; echo "错误信息是".MysqLi_error($this->link)."
"; } return $res; } //打印数据 public function p($arr){ echo "
";
    print_r($arr);
    echo "
"; } public function v($arr){ echo "
";
    var_dump($arr);
    echo "
"; } //获得最后一条记录id public function getInsertid(){ return MysqLi_insert_id($this->link); } /** * 查询某个字段 * @param * @return string or int */ public function getone($sql){ $query=$this->query($sql); return MysqLi_free_result($query); } //获取一行记录,return array 一维数组 public function getRow($sql,$type="assoc"){ $query=$this->query($sql); if(!in_array($type,array("assoc",'array',"row"))){ die("MysqLi_query error"); } $funcname="MysqLi_fetch_".$type; return $funcname($query); } //获取一条记录,前置条件通过资源获取一条记录 public function getFormSource($query,$type="assoc"){ if(!in_array($type,"array","row"))) { die("MysqLi_query error"); } $funcname="MysqLi_fetch_".$type; return $funcname($query); } //获取多条数据,二维数组 public function getAll($sql){ $query=$this->query($sql); $list=array(); while ($r=$this->getFormSource($query,"row")) { $list[]=$r; } return $list; } /** * 定义添加数据的方法 * @param string $table 表名 * @param string orarray $data [数据] * @return int 最新添加的id */ public function insert($table,$data){ //遍历数组,得到每一个字段和字段的值 $key_str=''; $v_str=''; foreach($data as $key=>$v){ if(empty($v)){ die("error"); } //$key的值是每一个字段s一个字段所对应的值 $key_str.=$key.','; $v_str.="'$v',"; } $key_str=trim($key_str,','); $v_str=trim($v_str,'); //判断数据是否为空 $sql="insert into $table ($key_str) values ($v_str)"; $this->query($sql); //返回上一次增加操做产生ID值 return $this->getInsertid(); } /* * 删除一条数据方法 * @param1 $table,$where=array('id'=>'1') 表名 条件 * @return 受影响的行数 */ public function deleteOne($table,$where){ if(is_array($where)){ foreach ($where as $key => $val) { $condition = $key.'='.$val; } } else { $condition = $where; } $sql = "delete from $table where $condition"; $this->query($sql); //返回受影响的行数 return MysqLi_affected_rows($this->link); } /* * 删除多条数据方法 * @param1 $table,$where 表名 条件 * @return 受影响的行数 */ public function deleteall($table,$where){ if(is_array($where)){ foreach ($where as $key => $val) { if(is_array($val)){ $condition = $key.' in ('.implode(',$val) .')'; } else { $condition = $key. '=' .$val; } } } else { $condition = $where; } $sql = "delete from $table where $condition"; $this->query($sql); //返回受影响的行数 return MysqLi_affected_rows($this->link); } /** * [修改操作description] * @param [type] $table [表名] * @param [type] $data [数据] * @param [type] $where [条件] * @return [type] */ public function update($table,$data,$where){ //遍历数组,得到每一个字段和字段的值 $str=''; foreach($data as $key=>$v){ $str.="$key='$v',"; } $str=rtrim($str,'); //修改sql语句 $sql="update $table set $str where $where"; $this->query($sql); //返回受影响的行数 return MysqLi_affected_rows($this->link); } /** * @func: 获取存储过程多条数据集 * @author: bieanju * @return: array * @createtime: 2017-12-25 */ public function getAllData($sql){ if (MysqLi_multi_query($this->link,$sql)) { do { if ($result = MysqLi_store_result($this->link)) { while ($row = MysqLi_fetch_assoc($result)) { $list[] = $row; } /*必须释放*/ MysqLi_free_result($result); }else{ return false; } /*MysqLi_next_result($this->link) && MysqLi_more_results($this->link)*/ } while (MysqLi_next_result($this->link) && MysqLi_more_results($this->link)); } else { return false; } return $list; } } ?>

2、存储过程调用扩展类库:

rush:PHP;"> MysqLi = new MysqLi($config); } /** * 根据参数绑定组装最终的sql语句 便于调试 * @access public * @param string $sql 带参数绑定的sql语句 * @param array $bind 参数绑定列表 * @return string */ private function getRealsql($sql,array $bind = []) { foreach ($bind as $key => $val) { $value = is_array($val) ? $val[0] : $val; $value = is_string($val) ? "'{$val}'" : $val; // 判断占位符 $sql = is_numeric($key) ? substr_replace($sql,$value,strpos($sql,'?'),1) : str_replace( [':' . $key . ')',':' . $key . ',':' . $key . ' '],[$value . ')',$value . ',$value . ' '],$sql . ' '); } return rtrim($sql); } /** * @func:存储过程执行并得到数据集 * @author: bieanju * @return: boolean * @createtime: 2017-12-22 */ protected function procs(){ $procedure = in_array(strtolower(substr(trim($this->sql),4)),['call','exec']); // 参数绑定 if ($procedure) { $sql = $this->getRealsql($this->sql,$this->data); return $this->MysqLi->getAllData($sql); } return false; } /** * @func: 存储过程数据 * @author: bieanju * @return: array * @createtime: 2017-12-22 */ public function data($data = []) { $this->data = $data; return $this; } /** * @func: 存储过程sql * @author: bieanju * @return: array * @createtime: 2017-12-22 */ public function sql($sql = '') { $this->sql = $sql; return $this; } /** * 使用DEMO */ public function demo(){ return $this->sql("call demo(?,?,?)")->procs(); } } ?>

3、最终项目中使用demo:

procs = new procs("MysqLi"); /*第二步调用demo方法获取数据*/ //$data为给存储过程占位符传递的参数必须为array|[ ] $this->procs->data($data)->demo();

ok是不是调用很简单、多条存储过程的数据集就此拿到!

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_form` (   `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
组合查询的主体还是采用数组方式查询,只是加入了一些特殊的查询支持,包括字符串模式查询(_string)、复合查询(_complex)、请求字符串查询(_query),混合查询中的特殊查询每次查询只能定义一个,由于采用数组的
(1)创建模版:/App/Home/View/Form/edit.html   <FORM method=\"post\" action=\"__URL__/update\">
自定义配置文件user.php: <?php return array(    \'sex\'=>\'man\', ); config.php: <?php return array(
在一些成熟的CMS系统中,后台一般都包含一个配置中心(如织梦后台中系统设置),以方便站长在后台修改配置文件;那么这个功能是如果实现的呢?在ThinkPHP中有没有捷径可走呢?答案肯定是有的。下面大概说一下这个功能
废话不多说先上图预览下,即本博客的分页; 这个分页类是在thinkphp框架内置的分页类的基础上修改而来,原分页类的一些设计,在实际运用中感觉不是很方便;
在php中截取字符串的函数有很多,而在thinkphp中也可以直接使用php的函数,本文给大家简单的介绍thinkPHP模板中截取字符串的具体用法,希望能对各位有所帮助。
thinkphp开发图片上传,图片异步上传是目前比较方便的功能,这里我就不写css文件了,将代码写出来。
配置数据库:/app/Common/Conf/config.php 方法一: // 添加数据库配置信息 \'DB_TYPE\'   => \'mysql\',// 数据库类型
/app/Home/Controller/IndexController.class.php
(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_data` (   `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
(1)控制器设置:/app/Home/Controller/IndexController.class.php <?php namespace HomeController; use ThinkController;
(1)普通模式 http://localhost/index.php?m=module&a=action&var=value m参数表示模块,a操作表示操作(模块和操作的URL参数名称是可以配置的),后面的表示其他GET参数。
入库的时候用htmlspecialchars()处理含有html代码的内容 输出的时候用htmlspecialchars_decode()处理含有html代码的内容
<?php define(\'APP_NAME\',\'app\'); define(\'APP_PATH\',\'./app/\'); define(\'APP_DEBUG\',TRUE); // 开启调试模式
(1)创建控制器中定义read方法:/App/Home/Controller/FormController.class.php public function read($id=0){
一、实现不同字段相同的查询条件 $User = M(\"User\"); // 实例化User对象 $map[\'name|title\'] = \'thinkphp\';
如果你的数据完全是内部操作写入而不是通过表单的话(也就是说可以充分信任数据的安全),那么可以直接使用add方法,如:
查询表达式的使用格式: $map[\'字段名\'] = array(\'表达式\',\'查询条件\'); 表达式不分大小写,支持的查询表达式有下面几种,分别表示的含义是:
一、使用字符串作为查询条件 $User = M(\"User\"); // 实例化User对象 $User->where(\'type=1 AND status=1\')->select();