MySQL Error - Commands out of sync; you can't run this command now

前端 未结 4 1052
一生所求
一生所求 2021-01-06 11:38

I am using MySQL with PHP, Codeigniter. I had a question which was answered by bluefeet in the post here

I created a stored procedure for the second solution by blue

相关标签:
4条回答
  • 2021-01-06 12:10

    Got the Answer! It seems like codeigniter's mysql driver has bugs handling stored procedures.

    I changed the drivers from mysql to mysqli in the config/database file by changing

    $db['default']['dbdriver'] = 'mysql';
    

    to

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

    Post that i modified the system/database/drivers/mysqli/mysqli_result.php file and added the below function

    function next_result()
    {
      if (is_object($this->conn_id))
      {
          return mysqli_next_result($this->conn_id);
      }
    }
    

    and modified the model as below

    $db = $this->load->database('mailbox',TRUE);
    $qry_res = $db->query('Call circle_pending_p()');
    
    echo $db->_error_message();
    $res = $qry_res->result_array();
    
    $qry_res->next_result();
    $qry_res->free_result();
    
    if (count($res) > 0) {
          return $res;
    } else {
          return 0;
    }
    

    This solved the problem!

    0 讨论(0)
  • 2021-01-06 12:10

    I faced similary problem when I was running stored procedure in a loop. The below code I was using:

    foreach ($month_list as $month_row) 
    {
    
      $start_date = $month_row->adate;
      $end_date = date("Y-m-d", strtotime("last day of " . date("F", strtotime($start_date)) . " " . date("Y", strtotime($start_date))));
    
      $qry_res = $this->db->query("call staff_attendance('$start_date', '$end_date')");
    
      $res = $qry_res->result();
    
      $qry_res->next_result();
      $qry_res->free_result();
    
      $compiled_months[] = $res;
    
    }
    

    In this case the temporary table I created was being complained as table already exists.

    So, used this approach instead:

    foreach ($month_list as $month_row) 
    {
    
      $start_date = $month_row->adate;
      $end_date = date("Y-m-d", strtotime("last day of " . date("F", strtotime($start_date)) . " " . date("Y", strtotime($start_date))));
    
      $compiled_months[] = $this->db->query("call staff_attendance('$start_date', '$end_date')")->result();
      $this->db->close();
      $this->db->initialize();
    
    }
    

    Now execution was perfect

    0 讨论(0)
  • 2021-01-06 12:13

    Try:

    <?php
    while($dbms->more_results() && $dbms->next_result())
        {
            $result = $dbms->store_result();
    
            if(is_object($result)){ $result->free(); }
    
            unset($result);
        }
    ?>
    

    After procedure call. MySQLi can't call another procedure, while has previous results. You should use free() for each of them before any further procedure call or query execution.

    Source: http://php.net/manual/en/mysqli.query.php

    0 讨论(0)
  • 2021-01-06 12:13

    In file

    system/database/drivers/mysqli/mysqli_result.php
    

    Add:

    function next_result()
    {
        if (is_object($this->conn_id))
        {
            return mysqli_next_result($this->conn_id);
        }
    }
    
    function result($type = 'object')
    {
        $result = array();
    
        if ($type == 'array') $result = $this->result_array();
        else if ($type == 'object') $result = $this->result_object();
        else $result = $this->custom_result_object($type);
    
        $this->next_result();
    
        return $result;
    }
    
    0 讨论(0)
提交回复
热议问题