深圳全飞鸿
标题:
php调用mysql存储过程,及Commands out of sync报错的实验分析(mysqli)
[打印本页]
作者:
zhgc
时间:
2020-6-7 00:06
标题:
php调用mysql存储过程,及Commands out of sync报错的实验分析(mysqli)
本帖最后由 zhgc 于 2021-3-21 14:28 编辑
php调用mysql存储过程,及Commands out of sync报错的实验分析
存储过程:
DELIMITER $
DROP PROCEDURE IF EXISTS `sfc`.`TEST2`$
CREATE DEFINER=`root`@`localhost` PROCEDURE `TEST2`(
)
BEGIN
declare l_count int;
-- 1. just update
update sfc.r107 set in_station_time=now() limit 12;
-- 2. return record;
select serial_number,model_name from sfc.r107 limit 3;
END$
DELIMITER ;
复制代码
PHP代码:
<?php
$con=mysqli_connect("127.0.0.1","root","****","sfc");
if (mysqli_connect_errno($con))
{
echo "连接 MySQL 失败: " . mysqli_connect_error();
}
$sql = "call sfc.TEST2();";
echo "以下为主数据:\r\n";
if($result = mysqli_query($con,$sql)){
if($result!==true){
while($row=mysqli_fetch_row($result)){
printf("%s \r\n",$row[0]);
}
mysqli_free_result($result);
}
}
$i=1;
echo "以下为附加数据:\r\n";
while(mysqli_more_results($con)) {
printf("--------- {$i} -----------\r\n");
if(mysqli_next_result($con)){
if ($result = mysqli_store_result($con)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s \r\n",$row[0]);
}
mysqli_free_result($result);
} else {
echo "no record!\r\n";
}
}
$i++;
}
mysqli_close($con);
?>
复制代码
作者:
zhgc
时间:
2020-6-7 00:08
当SP为:
update sfc.r107 set in_station_time=now() limit 12;
结果是:
C:\Apache24\htdocs\test>php t2.php
以下为主数据
以下为附加数据
复制代码
当SP为:
update sfc.r107 set in_station_time=now() limit 12;
select serial_number,model_name from sfc.r107 limit 3;\
结果是:
C:\Apache24\htdocs\test>php t2.php
以下为主数据:
19040171000001
19040171000002
19040171000003
以下为附加数据:
--------- 1 -----------
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;
结果是:
C:\Apache24\htdocs\test>php t2.php
以下为主数据:
19040171000001
19040171000002
19040171000003
以下为附加数据:
--------- 1 -----------
X000041
PF1642000VK
--------- 2 -----------
no record!
复制代码
作者:
zhgc
时间:
2020-6-7 00:10
所以结论是:
如果SP没有返回值 ,那没有问题
但一但有record ,那结果集是N+1 。多出来的是什么内容不清楚,读内容是空的,但如果不读,就会Commands out of sync
作者:
zhgc
时间:
2020-6-7 00:14
所以,建议调SP的处理方式 :
读主recrod:
if($result = mysqli_query($con,$sql)){
if($result!==true){
while($row=mysqli_fetch_row($result)){
printf("%s \r\n",$row[0]);
}
mysqli_free_result($result);
}
}
复制代码
清多余数据:
while(mysqli_more_results($con)) {
if(mysqli_next_result($con)){
if ($result = mysqli_store_result($con)) {
mysqli_free_result($result);
}
}
}
复制代码
作者:
zhgc
时间:
2020-6-7 00:31
完整代码 :
<?php
error_reporting(E_ALL);
$con=mysqli_connect("127.0.0.1","root","****","sfc");
if (!$con)
{
die( "连接 MySQL 失败: " . mysqli_connect_error());
}
$sql = "call sfc.TEST2();";
echo "以下为主数据:\r\n";
if($result = mysqli_query($con,$sql)){
if($result!==true){
while($row=mysqli_fetch_row($result)){
printf("%s \r\n",$row[0]);
}
mysqli_free_result($result);
}
}
while(mysqli_more_results($con)) {
if(mysqli_next_result($con)){
if ($result = mysqli_store_result($con)) {
mysqli_free_result($result);
}
}
}
$sql = "select version();";
if($result = mysqli_query($con,$sql)){
if($result!==true){
while($row=mysqli_fetch_row($result)){
printf("%s \r\n",$row[0]);
}
mysqli_free_result($result);
}
}else{
echo mysqli_error($con);
}
mysqli_close($con);
?>
微信图片_20200607003049.png
(8.95 KB, 下载次数: 660)
下载附件
2020-6-7 00:31 上传
作者:
zhgc
时间:
2023-10-26 15:47
$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()){};
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/)
Powered by Discuz! X3.2