在CI框架下执行存储的方法
我直接把代码摆在这里分享哈
<?php
/**
*
* Created by JetBrains PhpStorm.
* User: lsl
* Date: 14-1-8
* Time: 下午2:55
* To change this template use File | Settings | File Templates.
*/ define("QUERY_KEY", 1);
define("QUERY_INDEX", 2);
define("QUERY_BOTH", 3); /**
* 自定义数据库工具类,为了满足CI框架不能
* 在CI里面调用方式如下
* 1、在model的构造函数里面加载该类 $this->load->library('pdb');
* 2、在任一方法内调用方式如下
* $sqlParams[0] = new SqlParam('参数1',参数值1);
$sqlParams[1] = new SqlParam('参数2',参数2);
$sqlParams[2] = new SqlParam('@returnValue');参数返回值
$sqlParams[2]->Direction = Direction::$ReturnValue;
$procname = '存储过程名称';
$dt = $this->pdb->setDatabase("db_account")->runProcReturnTable($procname,$sqlParams,QUERY_KEY);
* $result = $sqlParams[2]->ParamValue;
* Class Pdb
*/
class Pdb { public $db_handler = array();
private $db_name; /**
* 构造函数
*/
function __construct(){
//TODO 这里暂时不需要做任何事情
} function &setDatabase($db_name){
$this->db_name = $db_name;
return $this;
} /**
* 析构函数
*/
function __destruct() { if(!is_null($this->db_handler[$this->db_name])){
$this->db_handler[$this->db_name]->close();//断开数据库连接
unset($this->db_handler[$this->db_name]);
}
} public function getDb(){
$db = array();
//加载配置文件
if ( file_exists($file_path = APPPATH.'config/'.'database.php'))
{
include($file_path);
} else {
show_error('The configuration file database.php does not exist.');
}
if(is_null($this->db_handler[$this->db_name])){
$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/>");
$this->db_handler[$this->db_name]->query("SET names ".$db[$this->db_name]['char_set']);
}
return $this->db_handler[$this->db_name];
} /**
* 获取单行结果集
* @param $sql
* @return mixed
*/
public function getRow($sql){
$db = $this->getDb();
$result = $db->query($sql);
$row = $result->fetch_array();
return $row;
} /**
* 执行sql语句,返回结果集
* @param $sql sql语句
* @return $result 返回结果集
*/
public function getResult($sql){
$db = $this->getDb();
$result = $db->query($sql);
return $result;
} /**
* 执行sql语句,返回结果集
* @param $sql sql语句
* @return $datatable array(0=>array("id"=>1),1=>array("id"=>2))
*/
public function returnDataTable($sql,$type = 3){
$db = $this->getDb();
$result = $db->query($sql);
if($result){
$row = $result->fetch_array($type);
$i=0;
while ($row){
$datatable[$i] = $row;
$i++;
$row = $result->fetch_array($type);
}
}
$datatable = $datatable?$datatable:array();
@mysqli_free_result($result);
return $datatable;
} /**
* 执行sql同时将结果集已多维数组形式返回
* @param $sql
* @param int $type
* @return mixed
*/
public function returnDataTables($sql,$type = 3){
$db = $this->getDb();
if($db->multi_query($sql)){
$j=0;
do{
$result = $db->store_result();
if ($result){
//获取第一个结果集
$row = $result->fetch_array($type);
$i=0;
while ($row){
$datatable[$j][$i] = $row;
$i++;
$row = $result->fetch_array($type);
}
$result->close(); //关闭一个打开的结果集
}
$j++;
} while($db->next_result());
}
@mysqli_free_result($result);
return $datatable;
} /**
* 通过存储参数获得sql语句
* @param $procname
* @param null $params
* @return string
*/
public function getProcSql($procname,$params=NULL){
$sql = "call ".$procname."(";
if($params){
$sqlOutPut = "select ";
for($i=0;$i<count($params);$i++){
if($params[$i]->Direction == Direction::$Output){
$sql .= $params[$i]->SqlParamName;
$sqlOutPut = $sqlOutPut.$params[$i]->SqlParamName.",";
$sql .= ",";
} else if($params[$i]->Direction == Direction::$Intput){
$sql .= "'".$params[$i]->ParamValue."',";
}
}
if(count($params)>0){
$sql = substr($sql, 0, strlen($sql)-1).");";
$sqlOutPut = substr($sqlOutPut, 0, strlen($sqlOutPut)-1).";";
}
}else {
$sql .= ");";
}
if(strlen($sqlOutPut)>7){
$sql .= $sqlOutPut;
}
return $sql;
} /**
* 执行存储同时返回结果集
* @param $procname
* @param null $params
* @param int $type
* @return array
*/
public function runProcReturnTable($procname,$params=NULL,$type = 3){
$db = $this->getDb();
//构建存储过程语句
$sql = $this->serializationProc($procname, $params,$db);
$result = $db->query($sql);
if($result){
$row = $result->fetch_array($type);
$i=0;
while ($row){
$datatable[$i] = $row;
$i++;
$row = $result->fetch_array($type);
}
}
$datatable = $datatable?$datatable:array();
@mysqli_free_result($result);
return $datatable;
} /**
* 执行存储过程
* @param string 存储过程名称
* @param array 参数数组 array(0=>SqlParam)
* @return string 返回构建的sql语句,用于调试
*/
public function runProc($procname,$params=NULL){
//执行存储过程,取回返回值与输出参数
$db = $this->getDb();
//构建存储过程语句
$sql = $this->serializationProc($procname, $params, $db);
if($db->multi_query($sql)){
$result = $db->store_result();
if($result){
$row = $result->fetch_array(2);
if($row){
for($i=0;$i<count($params);$i++){
if($params[$i]->Direction == Direction::$ReturnValue){
$params[$i]->ParamValue = $row[0];
}
}
}
}
do{
$result = $db->store_result();
if ($result) {
//获取第一个结果集
$row = $result->fetch_array(1);
for($i=0;$i<count($params);$i++){
if($params[$i]->Direction == Direction::$Output){
$params[$i]->ParamValue = $row[$params[$i]->SqlParamName];
}
}
$result->close(); //关闭一个打开的结果集
}
} while($db->next_result());
}
@mysqli_free_result($result);
return true;
} /**
* 序列号存储过程,将参数转换成sql的形式
* @param string 存储过程名称
* @param array 参数数组 array(0=>SqlParam)
* @param db 存储连接DB
* @return string 返回构建的sql语句
*/
private function serializationProc($procname,$params,&$db){
$sql = "call ".$procname."(";
if(count($params)>0){
$sqlOutPut = "select ";
foreach ($params as $v) {
if($v->Direction == Direction::$ReturnValue){
continue;
}
if(strpos($v->SqlParamName, "@") === FALSE){
$v->SqlParamName = "@".$v->SqlParamName;
}
$db->query("set ".$v->SqlParamName."='".$v->ParamValue."';");
$sql .= $v->SqlParamName;
$sql .= ",";
if($v->Direction == Direction::$Output){
$sqlOutPut .= $v->SqlParamName.",";
}
}
if(count($params)>0){
$sql = substr($sql, 0, strlen($sql)-1).");";
$sqlOutPut = substr($sqlOutPut, 0, strlen($sqlOutPut)-1).";";
}
}else {
$sql .= ");";
}
if(strlen($sqlOutPut)>7){
$sql .= $sqlOutPut;
}
return $sql;
} } /**
* 定义存储参数类型
* Class Direction
*/
class Direction{
public static $Intput = 1;
public static $Output = 2;
public static $ReturnValue = 3;
} /**
*
* Class SqlDBType
*/
class SqlDBType{
public static $Int = 1;
public static $Varchar = 2;
public static $DateTime = 3;
} /**
* 存储过程参数定义
* Class SqlParam
*/
class SqlParam{
public $Direction = 1;
public $SqlDBType = 1;
public $SqlParamName;
public $ParamValue;
public function SqlParam($ParamName = null,$ParamValue = null){
$this->SqlParamName = $ParamName;
if(!is_numeric($ParamName)){
$this->ParamValue = addslashes($ParamValue);
}else{
$this->ParamValue = $ParamValue;
}
}
public function setDirection($SqlDirection){
$this->Direction = $SqlDirection;
}
public function setSqlDBType($SqlDBType){
$this->SqlDBType=$SqlDBType;
}
public function setParamName($ParamName){
$this->ParamName=$ParamName;
}
public function setParamValue($ParamValue){
$this->ParamValue=$ParamValue;
}
}
在CI框架下执行存储的方法的更多相关文章
- CI 框架下执行CLI(命令行)
1.可以按照Ci官方文件的指导来进行操作 让我们先创建一个简单的控制器,打开你的文本编辑器,新建一个文件并命名为 Tools.php,然后输入如下的代码: <?php class Tools e ...
- Scrapy爬虫框架下执行爬虫的方法
在使用Scrapy框架进行爬虫时,执行爬虫文件的方法是 scrapy crawl xxx ,其中 xxx 是爬虫文件名. 但是,当我们在建立了多个文件时,使用上面的命令时会比较繁琐麻烦,我们就可以使用 ...
- CI框架下CSS和JS的路径问题
注意:CI框架下的CSS和JS的引用必须放在框架外面,比如,可建立resource文件夹与application同级,用来封装CSS和JS. 在view层用resource里面CSS和JS可采用以下几 ...
- 在shell下执行命令的方法
在shell下执行命令的方法 1. #!/bin/sh 语法:在shell.sh的开头写入 #!/bin/sh 一般的shell脚本就是这种用法.这种方法调用脚本开头的shell执行命令,子shell ...
- CI框架下 新浪微博登录接口完整版
https://www.cnblogs.com/yznyzcw/p/3756622.html#top 说明:本贴只适合CI框架.功能实现:登录接口跳转链接成功,获取用户信息(包括最重要的u_id)成功 ...
- 全面解析Pytorch框架下模型存储,加载以及冻结
最近在做试验中遇到了一些深度网络模型加载以及存储的问题,因此整理了一份比较全面的在 PyTorch 框架下有关模型的问题.首先咱们先定义一个网络来进行后续的分析: 1.本文通用的网络模型 import ...
- CI框架下的get_instance() 函数
你随便下个CI框架的源码都会看到很多的get_instance() 函数,这个函数是用来获取CI 的全局超级对象,CI 是单例模式的框架,所有全局有一个超级对象.因为只有一个实例,所以无论这个函数使用 ...
- CI框架下的PHP增删改查总结
controllers下的 cquery.php文件 <?php class CQuery extends Controller { //构造函数 function CQuery() { par ...
- PHP 之 Ci框架下隐藏index.php
1. 修改 apache 配置文件 开启重写模块 conf/httpd.conf 去掉前面的# LoadModule rewrite_module modules/mod_rewrite.so 对于U ...
随机推荐
- leetcode Trapping Rain Water pthon
class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 ...
- OC语法8——@class关键字
@class关键字: 在当前类中若要引用其他类的对象作成员变量(Book *book),我们以前采用的方式是 #import "Book.h" 但 #import "B ...
- Windows Server 2008 Enterprise使用12G内存
开启PAE选项,用以下命令解决这个问题: 1.开始运行cmd2.输入 BCDEdit /set PAE forceenable 后回车3.重起系统显示12GB内存,一切正常
- this指针与function变量--this究竟指向哪里?
参考文章:<深入浅出 JavaScript 中的 this> http://www.ibm.com/developerworks/cn/web/1207_wangqf_jsthis/ Ja ...
- python 内置错误类型 Built-in Exceptions
BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration ...
- MySQL----cluster安装
第一步.下载MySQL cluster: http://cdn.mysql.com/Downloads/MySQL-Cluster-7.4/mysql-cluster-gpl-7.4.7-linux- ...
- MySQL中用decimal的原因
原因:float保存的值有可能是这个值的近似值,而不是这个值的真实值.如 0.1在二进制中是没有办法保存真实值的. 下面是例子: 第一步:建表: create table t2(x float,y d ...
- 影响世界的IT
MIT BBS上说微软电话面试的一道题就是"Who do you think is the best coder,and why?”.我觉得挺有意思的,也来凑个热闹.排名不分先后. 1.Bi ...
- win7系统还原教程
当我们的win7系统出现故障了导致系统不能稳定运行而我们没有更好的解决办法时,我们一般的方式是对系统进行还原或重新安装win7系统了,本文主要讨论win7系统还原,抛开第三方软件不说,win7系统自带 ...
- [android]清单文件中MAIN与LAUNCHER的区别
原文:[android]清单文件中MAIN与LAUNCHER的区别 MAIN 和 LAUNCHER,之前一直不注意这两个有区别,写程序的时候都放到一个filter中,前两天面试问到了,总结一下: MA ...