在我的CI应用程序中设置为查询mssql
数据库。我想从active record
执行stored procedure
。但我无法掌握任何可靠的文件。从CodeIgniter的Active Record类中调用存储过程
有没有人有任何经验与CodeIgniter
和/或Active Record
调用存储特效并传递参数?
感谢,
比利
在我的CI应用程序中设置为查询mssql
数据库。我想从active record
执行stored procedure
。但我无法掌握任何可靠的文件。从CodeIgniter的Active Record类中调用存储过程
有没有人有任何经验与CodeIgniter
和/或Active Record
调用存储特效并传递参数?
感谢,
比利
是的,试试这个模型中。
$this->db->query("call {storedprocedure function name} ");
如果遇到故障投诉超过1个存储过程在同一时间,你需要添加下面一行
/* ADD THIS FUNCTION IN SYSTEM/DATABASE/DB_ACTIVE_REC */
/* USAGE $this->db->freeDBResource($this->db->conn_id); */
function freeDBResource($dbh){
while(mysqli_next_result($dbh)){
if($l_result = mysqli_store_result($dbh)){
mysqli_free_result($l_result);
}
}
}
我在/系统/数据库增加了以下功能类CI_DB_mysqli_driver /drivers/mysqli/mysqli_driver.php
function free_db_resource() { while(mysqli_next_result($this->conn_id)) { if($l_result = mysqli_store_result($this->conn_id)) { mysqli_free_result($l_result); } } }
和过程调用后使用它
$this->db->free_db_resource();
一个简单的方法来调用它的参数为您的存储过程是通过查询由笨的数据库库中提供()方法。
在你的模型: -
function call_procedure(){
$call_procedure ="CALL TestProcedure('$para1', '$para2', @para3)";
$this->db->query($call_procedure);
$call_total = 'SELECT @para3 as Parameter3';
$query = $this->db->query($call_total);
return $query->result();
}
如果您使用的是带有MSSQL或存储过程SQLSRV笨的更高版本,使用“CALL”为query('CALL procedureName($param1,$params,....)')
可能无法正常工作。
在MSSQL中的情况下使用:
$this->db->query('EXEC procedureName')
OR
$this->db->query('EXEC procedureName $param1 $param2 $param3,...')
在某些情况下,你可能需要打开一些常量的驱动程序。在这种情况下运行:
$this->db->query('Set MSSQL constant ON)
运行您的常规查询之前。
这个从上面的答案稍作修改。如果您正在使用笨3将此代码放在/system/database/drivers/mysqli/mysqli_driver.php:
function free_db_resource()
{
do
{
if($l_result = mysqli_store_result($this->conn_id))
{
mysqli_free_result($l_result);
}
}
while(mysqli_more_results($this->conn_id) && mysqli_next_result($this->conn_id));
}
然后就叫其他人一样的功能,这里建议。
感谢您的提示! – iamjonesy 2011-03-13 21:49:46
谢谢你,我几乎浪费了5个小时才弄清楚这个问题,最后我找到了正确的地方。代码中有一些错误来调用函数来释放对象,但我更改了“$ this-> db-> freeDBResource($ this-> db-> conn_id);”到“$ this-> freeDBResource($ this-> db-> conn_id);” ,它的工作完美 – 2016-04-30 20:57:49