2012-01-28 61 views
3

我这个工作代码CI:试图“叫”存储过程使用CodeIgniter

$this->db->query("call nameOfProcedure('param1', @param2)"); 
$query = $this->db->query('SELECT @param2 as results'); 
$row = $query->row(); 

它的工作原理,但如果我尝试使用:

$this->db->call_function('nameOfProcedure', 'param1', '@param2'); 

我得到的错误:

This feature is not available for the database you are using.

究竟是什么错误?

感谢

回答

4

检查出call_functiondocs。它用于调用不是CI的数据库驱动程序本机的函数,也不用于调用您编写的过程。

你可以在CI 2.1.0上查看/system/database/DB_driver.php Ln 998中的call_function代码,以清楚地看到它在做什么。

+0

不确定为什么你没有选择正确的答案。该文档清楚地说明它是用于调用** PHP **数据库函数。它没有提到存储过程。 – MikeMurko 2012-02-29 22:37:04

4

以防万一它帮助任何人。我使用这个库来处理CI中的存储过程,它也支持多个结果集。

这里是代码

我把它叫做Mydb.php

<?php #if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Mydb 
{ 
    private $CI, $Data, $mysqli, $ResultSet; 

    /** 
    * The constructor 
    */ 

    function __construct() 
    { 
    $this->CI =& get_instance(); 
    $this->Data = ''; 
    $this->ResultSet = array(); 
    $this->mysqli = $this->CI->db->conn_id; 
    } 

    public function GetMultiResults($SqlCommand) 
    { 
    /* execute multi query */ 
    if (mysqli_multi_query($this->mysqli, $SqlCommand)) { 
     $i=0; 
     do 
     { 

      if ($result = $this->mysqli->store_result()) 
      { 
       while ($row = $result->fetch_assoc()) 
       { 
        $this->Data[$i][] = $row; 
       } 
       mysqli_free_result($result); 
      } 
      $i++; 
     } 
     while ($this->mysqli->next_result()); 
    } 
    return $this->Data; 

    } 
} 
?> 

调用它像这样从控制器

$this->load->library('mydb'); 
$arr = $this->mydb->GetMultiResults("CALL GetReferrals()"); 

此外,请务必设置mysqli司机 在application/config/database.php

$db['default']['dbdriver'] = 'mysqli'; 
+0

库仅支持返回多个结果集,并且如果某个过程只返回一个结果ret,则会抛出错误。为了还支持返回一个结果返回,必须改变行“while($ this-> mysqli-> next_result());”到“while($ this-> mysqli-> more_results()&& $ this-> mysqli-> next_result());”。 – Stony 2015-11-06 21:36:35

+0

@Stony它是一个do-while循环,你测试它不工作吗? – Junaid 2015-11-17 13:02:27