• 在CI框架下执行存储的方法


    我直接把代码摆在这里分享哈

      1 <?php
      2 /**
      3  *
      4  * Created by JetBrains PhpStorm.
      5  * User: lsl
      6  * Date: 14-1-8
      7  * Time: 下午2:55
      8  * To change this template use File | Settings | File Templates.
      9  */
     10 
     11 define("QUERY_KEY", 1);
     12 define("QUERY_INDEX", 2);
     13 define("QUERY_BOTH", 3);
     14 
     15 /**
     16  * 自定义数据库工具类,为了满足CI框架不能
     17  * 在CI里面调用方式如下
     18  * 1、在model的构造函数里面加载该类 $this->load->library('pdb');
     19  * 2、在任一方法内调用方式如下
     20  *  $sqlParams[0] = new SqlParam('参数1',参数值1);
     21     $sqlParams[1] = new SqlParam('参数2',参数2);
     22     $sqlParams[2] = new SqlParam('@returnValue');参数返回值
     23     $sqlParams[2]->Direction = Direction::$ReturnValue;
     24     $procname = '存储过程名称';
     25     $dt = $this->pdb->setDatabase("db_account")->runProcReturnTable($procname,$sqlParams,QUERY_KEY);
     26  *  $result = $sqlParams[2]->ParamValue;
     27  * Class Pdb
     28  */
     29 class Pdb  {
     30 
     31     public  $db_handler = array();
     32     private $db_name;
     33 
     34     /**
     35      * 构造函数
     36      */
     37     function __construct(){
     38         //TODO 这里暂时不需要做任何事情
     39     }
     40 
     41     function &setDatabase($db_name){
     42         $this->db_name = $db_name;
     43         return $this;
     44     }
     45 
     46     /**
     47      * 析构函数
     48      */
     49     function __destruct() {
     50 
     51         if(!is_null($this->db_handler[$this->db_name])){
     52             $this->db_handler[$this->db_name]->close();//断开数据库连接
     53             unset($this->db_handler[$this->db_name]);
     54         }
     55     }
     56 
     57     public function getDb(){
     58         $db = array();
     59         //加载配置文件
     60         if (  file_exists($file_path = APPPATH.'config/'.'database.php'))
     61         {
     62             include($file_path);
     63         } else {
     64             show_error('The configuration file database.php does not exist.');
     65         }
     66         if(is_null($this->db_handler[$this->db_name])){
     67             $this->db_handler[$this->db_name] = mysqli_connect($db[$this->db_name]['hostname'], $db[$this->db_name]['username'],$db[$this->db_name]['password'],$db[$this->db_name]['database']) or die("Could not connect: " . mysql_error() . "<br/>");
     68             $this->db_handler[$this->db_name]->query("SET names ".$db[$this->db_name]['char_set']);
     69         }
     70         return $this->db_handler[$this->db_name];
     71     }
     72 
     73     /**
     74      * 获取单行结果集
     75      * @param $sql
     76      * @return mixed
     77      */
     78     public function getRow($sql){
     79         $db = $this->getDb();
     80         $result = $db->query($sql);
     81         $row = $result->fetch_array();
     82         return $row;
     83     }
     84 
     85     /**
     86      * 执行sql语句,返回结果集
     87      * @param $sql  sql语句
     88      * @return  $result 返回结果集
     89      */
     90     public function getResult($sql){
     91         $db = $this->getDb();
     92         $result = $db->query($sql);
     93         return $result;
     94     }
     95 
     96     /**
     97      * 执行sql语句,返回结果集
     98      * @param $sql  sql语句
     99      * @return  $datatable array(0=>array("id"=>1),1=>array("id"=>2))
    100      */
    101     public function returnDataTable($sql,$type = 3){
    102         $db = $this->getDb();
    103         $result = $db->query($sql);
    104         if($result){
    105             $row = $result->fetch_array($type);
    106             $i=0;
    107             while ($row){
    108                 $datatable[$i] = $row;
    109                 $i++;
    110                 $row = $result->fetch_array($type);
    111             }
    112         }
    113         $datatable = $datatable?$datatable:array();
    114         @mysqli_free_result($result);
    115         return $datatable;
    116     }
    117 
    118     /**
    119      * 执行sql同时将结果集已多维数组形式返回
    120      * @param $sql
    121      * @param int $type
    122      * @return mixed
    123      */
    124     public function returnDataTables($sql,$type = 3){
    125         $db = $this->getDb();
    126         if($db->multi_query($sql)){
    127             $j=0;
    128             do{
    129                 $result = $db->store_result();
    130                 if ($result){
    131                     //获取第一个结果集
    132                     $row = $result->fetch_array($type);
    133                     $i=0;
    134                     while ($row){
    135                         $datatable[$j][$i] = $row;
    136                         $i++;
    137                         $row = $result->fetch_array($type);
    138                     }
    139                     $result->close();               //关闭一个打开的结果集
    140                 }
    141                 $j++;
    142             } while($db->next_result());
    143         }
    144         @mysqli_free_result($result);
    145         return $datatable;
    146     }
    147 
    148 
    149     /**
    150      * 通过存储参数获得sql语句
    151      * @param $procname
    152      * @param null $params
    153      * @return string
    154      */
    155     public function getProcSql($procname,$params=NULL){
    156         $sql = "call ".$procname."(";
    157         if($params){
    158             $sqlOutPut = "select ";
    159             for($i=0;$i<count($params);$i++){
    160                 if($params[$i]->Direction == Direction::$Output){
    161                     $sql .= $params[$i]->SqlParamName;
    162                     $sqlOutPut = $sqlOutPut.$params[$i]->SqlParamName.",";
    163                     $sql .= ",";
    164                 }  else if($params[$i]->Direction == Direction::$Intput){
    165                     $sql .= "'".$params[$i]->ParamValue."',";
    166                 }
    167             }
    168             if(count($params)>0){
    169                 $sql = substr($sql, 0,  strlen($sql)-1).");";
    170                 $sqlOutPut = substr($sqlOutPut, 0,  strlen($sqlOutPut)-1).";";
    171             }
    172         }else {
    173             $sql .= ");";
    174         }
    175         if(strlen($sqlOutPut)>7){
    176             $sql .= $sqlOutPut;
    177         }
    178         return $sql;
    179     }
    180 
    181     /**
    182      * 执行存储同时返回结果集
    183      * @param $procname
    184      * @param null $params
    185      * @param int $type
    186      * @return array
    187      */
    188     public function runProcReturnTable($procname,$params=NULL,$type = 3){
    189         $db = $this->getDb();
    190         //构建存储过程语句
    191         $sql = $this->serializationProc($procname, $params,$db);
    192         $result = $db->query($sql);
    193         if($result){
    194             $row = $result->fetch_array($type);
    195             $i=0;
    196             while ($row){
    197                 $datatable[$i] = $row;
    198                 $i++;
    199                 $row = $result->fetch_array($type);
    200             }
    201         }
    202         $datatable = $datatable?$datatable:array();
    203         @mysqli_free_result($result);
    204         return $datatable;
    205     }
    206 
    207     /**
    208      * 执行存储过程
    209      * @param string     存储过程名称
    210      * @param array       参数数组 array(0=>SqlParam)
    211      * @return string         返回构建的sql语句,用于调试
    212      */
    213     public function runProc($procname,$params=NULL){
    214         //执行存储过程,取回返回值与输出参数
    215         $db = $this->getDb();
    216         //构建存储过程语句
    217         $sql = $this->serializationProc($procname, $params, $db);
    218         if($db->multi_query($sql)){
    219             $result = $db->store_result();
    220             if($result){
    221                 $row = $result->fetch_array(2);
    222                 if($row){
    223                     for($i=0;$i<count($params);$i++){
    224                         if($params[$i]->Direction == Direction::$ReturnValue){
    225                             $params[$i]->ParamValue = $row[0];
    226                         }
    227                     }
    228                 }
    229             }
    230             do{
    231                 $result = $db->store_result();
    232                 if ($result) {
    233                     //获取第一个结果集
    234                     $row = $result->fetch_array(1);
    235                     for($i=0;$i<count($params);$i++){
    236                         if($params[$i]->Direction == Direction::$Output){
    237                             $params[$i]->ParamValue = $row[$params[$i]->SqlParamName];
    238                         }
    239                     }
    240                     $result->close();               //关闭一个打开的结果集
    241                 }
    242             } while($db->next_result());
    243         }
    244         @mysqli_free_result($result);
    245         return true;
    246     }
    247 
    248     /**
    249      * 序列号存储过程,将参数转换成sql的形式
    250      * @param string     存储过程名称
    251      * @param array       参数数组 array(0=>SqlParam)
    252      * @param db           存储连接DB
    253      * @return string      返回构建的sql语句
    254      */
    255     private function serializationProc($procname,$params,&$db){
    256         $sql = "call ".$procname."(";
    257         if(count($params)>0){
    258             $sqlOutPut = "select ";
    259             foreach ($params as $v) {
    260                 if($v->Direction == Direction::$ReturnValue){
    261                     continue;
    262                 }
    263                 if(strpos($v->SqlParamName, "@") === FALSE){
    264                     $v->SqlParamName = "@".$v->SqlParamName;
    265                 }
    266                 $db->query("set ".$v->SqlParamName."='".$v->ParamValue."';");
    267                 $sql .= $v->SqlParamName;
    268                 $sql .= ",";
    269                 if($v->Direction == Direction::$Output){
    270                     $sqlOutPut .= $v->SqlParamName.",";
    271                 }
    272             }
    273             if(count($params)>0){
    274                 $sql = substr($sql, 0,  strlen($sql)-1).");";
    275                 $sqlOutPut = substr($sqlOutPut, 0,  strlen($sqlOutPut)-1).";";
    276             }
    277         }else {
    278             $sql .= ");";
    279         }
    280         if(strlen($sqlOutPut)>7){
    281             $sql .= $sqlOutPut;
    282         }
    283         return $sql;
    284     }
    285 
    286 }
    287 
    288 /**
    289  * 定义存储参数类型
    290  * Class Direction
    291  */
    292 class Direction{
    293     public static $Intput = 1;
    294     public static $Output = 2;
    295     public static $ReturnValue = 3;
    296 }
    297 
    298 /**
    299  *
    300  * Class SqlDBType
    301  */
    302 class SqlDBType{
    303     public static $Int = 1;
    304     public static $Varchar = 2;
    305     public static $DateTime = 3;
    306 }
    307 
    308 /**
    309  * 存储过程参数定义
    310  * Class SqlParam
    311  */
    312 class SqlParam{
    313     public $Direction = 1;
    314     public $SqlDBType = 1;
    315     public $SqlParamName;
    316     public $ParamValue;
    317     public function SqlParam($ParamName = null,$ParamValue = null){
    318         $this->SqlParamName = $ParamName;
    319         if(!is_numeric($ParamName)){
    320             $this->ParamValue = addslashes($ParamValue);
    321         }else{
    322             $this->ParamValue = $ParamValue;
    323         }
    324     }
    325     public function setDirection($SqlDirection){
    326         $this->Direction = $SqlDirection;
    327     }
    328     public function setSqlDBType($SqlDBType){
    329         $this->SqlDBType=$SqlDBType;
    330     }
    331     public function setParamName($ParamName){
    332         $this->ParamName=$ParamName;
    333     }
    334     public function setParamValue($ParamValue){
    335         $this->ParamValue=$ParamValue;
    336     }
    337 }
    现主要从事PHP、Uinx/Linux、C/C++等方面的项目开发。
  • 相关阅读:
    C++里面关于虚函数的一些注意点
    HDOJ 2736 Surprising Strings
    hdu
    hdu 4404 Worms(多边形与圆的交)
    HTC one/M7电信802d 毒蛇ViperOne2.1.0/高级毒蛇工具/完美root,精简/更多自定义,稳定,流畅ROM
    Replace
    Centos环境下部署游戏服务器-权限
    jdk1.6与1.7垃圾回收
    java--基于socket的网络传输开发
    番外:android模拟器连不上网
  • 原文地址:https://www.cnblogs.com/lsl8966/p/3510710.html
Copyright © 2020-2023  润新知