php - CodeIgniter 存储过程的多个结果

标签 php mysql codeigniter

我在MySQL上有一个存储过程,例如:

CREATE PROCEDURE get_multiple_results()
BEGIN
  SELECT 'A' AS A;
  SELECT 'B' AS B;
  SELECT 'C' AS C;
END

那么,如何使用CodeIgniter的查询方式获取数据呢?

$this->db->query('CALL get_multiple_results()')->result_array();

谢谢!

最佳答案

只需调用下面编写的方法并获取查询结果的数组(列表),例如 $resultSet = $this->GetMultipleQuery("CALL my_proc ('$input')");

查询字符串也可以是多个选择查询的串联。

     /**
     * To get result(s) of queries that returns multiple result sets...
     *
     * @author Pankaj Garg <garg.pankaj15@gmail.com>
     *
     * @param string $queryString
     *
     * @return bool|array List of result arrays
     */
public function GetMultipleQueryResult($queryString)
{
    if (empty($queryString)) {
                return false;
            }

    $index     = 0;
    $ResultSet = array();

    /* execute multi query */
    if (mysqli_multi_query($this->db->conn_id, $queryString)) {
        do {
            if (false != $result = mysqli_store_result($this->db->conn_id)) {
                $rowID = 0;
                while ($row = $result->fetch_assoc()) {
                    $ResultSet[$index][$rowID] = $row;
                    $rowID++;
                }
            }
            $index++;
        } while (mysqli_next_result($this->db->conn_id));
    }

    return $ResultSet;
}

关于php - CodeIgniter 存储过程的多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18000704/

相关文章:

javascript - 用正则表达式剥离CSS?

php - 依次加载 IFrame

php - 如何将数组值与 codeigniter 数据库查询中的列进行比较

mysql - MySQL 调查设计中的可重用问题

php - CodeIgniter 3 + SQL Server + Windows 10

php - 使用 CodeIgniter 在 View 文件夹外加载 View

PHP - 如何将全局变量传递给函数

php - PHP foreach后返回不同的输入

php - 将变量从 shell exec 传递到 mysql_query

mysql - 我应该在哪里添加 codeigniter 模型中的 where 子句?