调用多个存储过程时发生php mysql错误

tvokkenx  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(299)

首先我要解释一下我的工作环境。我正在使用 Windows 7, CodeIgniter 3.0.2, PHP 5.2.0 and MySQL 5.5 . 我面临着困难,同时呼吁两个不同的程序一个接一个。从第一个程序,我得到的房间可用性,并基于这一点,我呼吁另一个程序。第一个过程运行正常,但在调用第二个过程时,错误显示为:
命令不同步;现在不能运行此命令

CALL ConfirmRoom('40331411072018', 5,3, '2018-07-02', '2018-07-04', '27062018I10023',1)

我提到下面的代码:

//1st Procedure
$sql = $this->db->query("CALL CheckAvailbility(GuestHouse,RoomType,'StayDate','StayOutDate',room)");
if($sql->num_rows()>0) {
  $row = $sql->first_row();
  if($row->avail='Y') {
    //2nd Procedure
    $sql = $this->db->query("CALL ConfirmRoom('BookingId', GuestHouse,RoomType, 'StayDate', 'StayOutDate', 'GuestID',noroom)");
    if($sql->num_rows()>0) {
      //statement
    }
  }
}
else {
  //statement
}
3xiyfsfu

3xiyfsfu1#

我以前遇到过它,这就是我解决它的方法(可能不是最好的解决方案,但它是有效的):

$this->db->close();
$this->db->initialize();

$test = $this->db->query("CALL sp_testProcedure(0, 1)");

$this->db->close();
$this->db->initialize();

$test2 = $this->db->query("CALL sp_testProcedure2(2, 3)"); 

$this->db->close();
$this->db->initialize();
i7uq4tfw

i7uq4tfw2#

我认为问题在于试图从对象($this)同时使用两个数据库函数。而不是将另一个可靠的“if语句”串联起来。尝试创建一个变量来存储“if语句”之外的结果计数,以确定所需的响应。
例如:

class Guest{

    private $_count= 0;

    public function count(){
        return $this->_count;
    }

}

//1st Procedure
$sql = $this->db->query("CALL 
    CheckAvailbility(GuestHouse,RoomType,'StayDate','StayOutDate',room)");
    if($sql->num_rows()>0) {
        $this->$_count++
        //have the $_count value auto increment to say the room is available
    }

if($this->count === 1) {
//2nd Procedure
$sql = $this->db->query("CALL ConfirmRoom('BookingId', GuestHouse,RoomType, 
    'StayDate', 'StayOutDate', 'GuestID',noroom)");
    if($sql->num_rows()>0) {
        //statement
        }
      }
else {
  //statement
}

如果我的例子有意义的话。。。。。。我不知道您的代码的布局,但这里有一个使用类变量存储结果的示例,这样可以连续调用数据库,而不是同时调用。

相关问题