深圳全飞鸿

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz

php调用mysql存储过程,及Commands out of sync报错的实验分析(mysqli)

查看数: 864 | 评论数: 5 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2020-6-7 00:06

正文摘要:

本帖最后由 zhgc 于 2021-3-21 14:28 编辑 php调用mysql存储过程,及Commands out of sync报错的实验分析 存储过程: DELIMITER $ DROP PROCEDURE IF EXISTS `sfc`.`TEST2`$ CREATE DEFINER=`root`@`localh ...

回复

zhgc 发表于 2023-10-26 15:47:46
$strsql="CALL smt.APP_SMT_SCAN('{$trsn}','{$emp}','{$machine}','{$action}','{$device}');";
                $info=$info."<p style=\"margin-top:0px;margin-bottom:0px;\"><b>执行操作:</b>{$strsql}</p>";
                $this->app->AjaxAttr("info",$info);
               
                $this->app->prepare_mes();
                $recordSet=$this->app->mes->execute($strsql);
                if(isset($recordSet->fields)){
                        if($recordSet->EOF){
                                $msg= "SQL没有返回数据";
                        }else{
                                $msg= "SQL没有RES返回参数";
                                $rs=$recordSet->fields;
                                if (array_key_exists('prompt', $rs)){
          $prompt=$this->app->LL($rs["prompt"]);
                                }
                                if (array_key_exists('machine', $rs)){
          $machine=$rs["machine"];
                                }
                                if (array_key_exists('res', $rs)){
          $msg=$rs["res"];
                                }
                        }
                }else{
                        $msg="数据库连接失败!".strToUtf8($this->app->mes->ErrorMsg());
                };


                $this->app->AjaxAttr("prompt",$prompt);
                $this->app->AjaxAttr("machine",$machine);
                if(substr($msg,0,2)=="OK"){
                        $this->app->AjaxAttr("sound","0");
                        $this->app->AjaxOK(array(),$msg);
                }else{
                        $this->app->AjaxAttr("sound","1");
                        $this->app->AjaxFail("{$msg}", 100);
                        return ;
                }
               
                if(strlen($trsn)<5){
                        return;
                }
                 while($recordSet->nextRecordSet()){};
zhgc 发表于 2020-6-7 00:14:46
所以,建议调SP的处理方式 :


读主recrod:
  1. if($result = mysqli_query($con,$sql)){
  2.         if($result!==true){
  3.                 while($row=mysqli_fetch_row($result)){
  4.                         printf("%s \r\n",$row[0]);
  5.                 }
  6.                 mysqli_free_result($result);
  7.         }
  8. }
复制代码




清多余数据:
  1. while(mysqli_more_results($con)) {
  2.         if(mysqli_next_result($con)){
  3.                 if ($result = mysqli_store_result($con)) {
  4.                         mysqli_free_result($result);
  5.                 }
  6.         }
  7. }
复制代码


zhgc 发表于 2020-6-7 00:10:42
所以结论是:

如果SP没有返回值 ,那没有问题
但一但有record ,那结果集是N+1 。多出来的是什么内容不清楚,读内容是空的,但如果不读,就会Commands out of sync
zhgc 发表于 2020-6-7 00:08:41
当SP为:
update sfc.r107 set in_station_time=now() limit 12;

结果是:
  1. C:\Apache24\htdocs\test>php t2.php
  2. 以下为主数据
  3. 以下为附加数据
复制代码



当SP为:
update sfc.r107 set in_station_time=now() limit 12;
select serial_number,model_name from sfc.r107 limit 3;\

结果是:
  1. C:\Apache24\htdocs\test>php t2.php
  2. 以下为主数据:
  3. 19040171000001
  4. 19040171000002
  5. 19040171000003
  6. 以下为附加数据:
  7. --------- 1 -----------
  8. no record!
复制代码



当SP为:
update sfc.r107 set in_station_time=now() limit 12;
select serial_number,model_name from sfc.r107 limit 3;\
select serial_number from sfc.r109 limit 2;

结果是:
  1. C:\Apache24\htdocs\test>php t2.php
  2. 以下为主数据:
  3. 19040171000001
  4. 19040171000002
  5. 19040171000003
  6. 以下为附加数据:
  7. --------- 1 -----------
  8. X000041
  9. PF1642000VK
  10. --------- 2 -----------
  11. no record!
复制代码

QQ|Archiver|手机版|小黑屋|nagomes  

GMT+8, 2025-5-5 03:59 , Processed in 0.028651 second(s), 26 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表