如何解决mysql 中出现Commands out of sync; you can't run this command now

出现此句的中文意思为:命令不同步,你不能运行此命令.出现此问题是在同一个mysqli连接中同时调用了二次MySQL的过程,对于此问题,在网上查找了相当的资料,有的说要通过mysqli::multi_query()方法来分别执行不同的过程,但是通过此方法经研究后发现其实不能解决问题.并且,mysqli::multi_query()对于存贮过程的调用显然没有mysqli_stmt容易和方便.
之后,终于发现了mysqli::next_result()的方法:(此前一直以为是获取mysqli的下一个结果集的方法),但查看了手册才知道其原意为:
Prepare next result from multi_query 为多重查询准备下一个结果集,
好象找到解决问题的方法了:
if ($link->next_result()){
  //执行新的mysqli查询
}
代码修改如下:
<?php
  class destroyobj3{
    private $link;
 private $id;
 private $name;
 private $pwd;
 private $ip;
 private $mail;
 private $mobile;
 private $phone;
 public function __construct($pid){
   if(!is_integer($pid)){
     throw new Exception("Parameter must be integer");
  exit();
   }
   $this->id=$pid;
  
   $this->link=new mysqli('localhost','root','750901lanpioneer@!','test');
   $this->link->query("set names 'utf8'");
   if($stmt=$this->link->prepare("call UP_Get_tuser(?)")){
     $stmt->bind_param("i",$this->id);
  $stmt->execute();
  $stmt->bind_result($this->id,$this->name,$this->pwd,$this->ip,$this->mail,$this->mobile,$this->phone);
  $stmt->fetch();
  $stmt->free_result();
  $stmt->close();
   }
  
 }
 public function getName(){
   return $this->name;
 }
 public function setName($pname){
   $this->name=$pname;
 }
 public function getPwd(){
   return $this->pwd;
 }
 public function setPwd($pwd){
   $this->pwd=md5($pwd);
 }
 public function getMail(){
   return $this->mail;
 }
 public function setMail($mail){
   $this->mail=$mail;
 }
 public function getMobile(){
   return $this->mobile;
 }
 public function setMobile($mobile){
   $this->mobile=$mobile;
 }
 public function getPhone(){
   return $this->phone;
 }
 public function setPhone($phone){
   $this->phone=$phone;
 }
    public function __destruct(){
   //注意:此句相当重要:
   if($this->link->next_result()){
     $this->link->query("set names 'utf8'") or die($this->link->error);
        $stmt=$this->link->prepare("call UP_Update_tuser(?,?,?,?,?,?,?)") or die ("error".$this->link->error);
    
  $stmt->bind_param("issssss",$this->id,$this->name,$this->pwd,$this->ip,$this->mail,$this->mobile,$this->phone);
        $stmt->execute();
  $stmt->fetch();
  $stmt->free_result();
        $stmt->close();
  }
  $this->link->close(); 
  
 }
 }
?>
果然解决问题!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值