如何在codeigniter中使用存储过程的结果集

时间:2018-01-16 12:54:28

标签: php codeigniter stored-procedures

如何在单个存储过程中执行多个查询,然后使用我们模型中所有查询获得的结果。我得到查询错误:命令不同步;尝试使用存储过程的结果时,您无法运行此命令错误。这是我的代码:

**模特:**

    /* convert post array data to string */
    $pr = "'" .implode("', '", $data) . "'";
    $pr2 = "'" .implode("', '", $data2) . "'";
    $pr5 = "'" .implode("', '", $data4) . "'";

    /* call stored procedure */
    $this->db->query('CALL addCustomerSalesData("'.$pr.'", "'.$pr2.'", "'.$pr5.'", @CustSalesID, @CustSalesProID)');

    /* get the stored procedure returned output */
    $query = $this->db->query('SELECT @CustSalesID AS cust_sales_id, @CustSalesProID AS cust_sales_pro_id');
    $row = $query->row_array();

    /* this will release memory which is used by stored procedure and make it free for another process */
    $query->next_result();
    $query->free_result();

    /* return inserted id*/
    return $row;

2 个答案:

答案 0 :(得分:0)

你在一个中运行两个查询。所以mysql消耗更多内存来保存它们

$this->db->query('CALL addCustomerSalesData("'.$pr.'", "'.$pr2.'", "'.$pr5.'", @CustSalesID, @CustSalesProID)');

$query = $this->db->query('SELECT @CustSalesID AS cust_sales_id, @CustSalesProID AS cust_sales_pro_id');

你能做什么

  1. 先存储并执行第二个
  2. 在两个查询之间
  3. $query->free_result();释放那些

答案 1 :(得分:0)

如果要在存储过程中使用多个select,以避免命令不同步;您现在无法运行此命令错误您需要做的是:

找到此文件: 系统/数据库/驱动器/库MySQLi / mysqli_driver.php

在此文件中找到函数:function _execute($ sql){}

将其更改为:

protected function _execute($sql)
{
    // Free result from previous query
    @mysqli_free_result($this->result_id);

    $sql = $this->_prep_query($sql);

    // get a result code of query (), can be used for test is the query ok
    $retval = @mysqli_multi_query($this->conn_id, $sql);

    // get a first resultset
    $firstResult = @mysqli_store_result($this->conn_id);

    // free other resultsets
    while (@mysqli_next_result($this->conn_id)) {
        $result = @mysqli_store_result($this->conn_id);
        @mysqli_free_result($result);
    }

    // test is the error occur or not
    if (!$firstResult && !@mysqli_errno($this->conn_id)) {
        return true;
    }
    return $firstResult;
}