CodeIgniter active records' problems calling m

2019-06-26 07:43发布

问题:

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();   
    }

this is my controller code which calls three functions from model one by one:

$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);

my problem is when i run only one function it runs successfully but when i run two of them or all three it shows blank page... any idea why ???

SOLUTION

So finally the only solution we got for my own problem is :

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

回答1:

The problem is related to CodeIgniter's active recors and multiple database stored procedure calling.

First of all check that dbdriver parameter (application/config/database.php) is set to mysqli. Then, as described in "Calling a stored procedure from CodeIgniter's Active Record class" question on StackOverflow, adding to system/database/DB_active_rec.php the following function:

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

..And in your controller use:

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

after any stored procedure calling.



回答2:

Model and Controller seems to be ok. If you try a model like:

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');
   }
}

...And call it functions from a controller like this:

$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);

You can see multiple function calling working fine.

Are you sure you can execute any of three function in model correctly, if you call only one? If yes, maybe the problem can be find in your stored procedures... Can you try to execute a normal query instead of stored procedures, in model functions? For debug your problem, check also in your /application/config/database.php if db_debug is set to TRUE.