代码点火器活动记录调用多个存储过程时遇到的问题


class Registration_model extends CI_Model {
    function __construct() {
        parent::__construct();
    }   
    function check_email_availability($email)
    {
        $sql = "CALL proc_1301('$email');"; 
        $query = $this->db->query($sql) or die(mysql_error()); 
        return $query->row_array();
    }
    function check_username_availability($username)
    {
        $sqlt = "CALL proc_1303('$username');"; 
        $query = $this->db->query($sqlt) or die(mysql_error()); 
        return $query->row_array();
    }
    function process_registration($username, $email, $password)
    { 
        $sql = "CALL `proc_1302`('$username', '$email', '$password');"; 
        $query = $this->db->query($sql) or die(mysql_error()); 
        return $query->result_array();   
    }

这是我的控制器代码,它逐个调用模型中的三个函数:

$emailCheckRes = $this->Registration_model->check_email_availability($email); 
$usernameCheckRes = $this->Registration_model->check_username_availability($username); 
$this->data['regRes'] = $this->Registration_model->process_registration($username, $email, $password);
我的问题是当我只运行一个函数时,它成功运行,

但是当我运行其中两个或所有三个函数时,它会显示空白页......知道为什么???

溶液

所以最后我们为自己的问题得到的唯一解决方案是:

/* 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);
            }
        }
}

该问题与 CodeIgniter 的活动 recors 和多数据库存储过程调用有关。

首先检查 dbdriver 参数(application/config/database.php(是否设置为 mysqli。然后,如 StackOverflow 上的"从 CodeIgniter 的活动记录类调用存储过程"问题中所述,向系统/数据库/DB_active_rec.php添加以下函数:

function freeDBResource($dbh){
    while(mysqli_next_result($dbh)){
            if($l_result = mysqli_store_result($dbh)){
              mysqli_free_result($l_result);
            }
        }
}

..在您的控制器中使用:

$this->db->freeDBResource($this->db->conn_id);

在任何存储过程调用之后。

模型和控制器似乎还可以。如果您尝试以下模型:

class Test_model extends CI_Model
{
   function __construct()
   {
      parent::__construct();
   }
   function a($a_input)
   {
      return($a_input.': a');
   }
   function b($b_input)
   {
     return($b_input.': b');
   }
}

。并从控制器调用它函数,如下所示:

$this->load->model('Test_model');
    $a_result = $this->Test_model->a('aaa');
    $b_result = $this->Test_model->b('bbb');
    echo($a_result.'<br />'.$b_result);

您可以看到多个函数调用工作正常。

如果您只调用一个函数,您确定可以正确执行模型中三个函数中的任何一个吗?如果是,也许可以在存储过程中找到问题...是否可以尝试在模型函数中执行普通查询而不是存储过程?要调试您的问题,请在您的/application/config/database 中检查.php db_debug是否设置为 TRUE。

最新更新