循环准备的声明

时间:2017-03-03 17:42:22

标签: php mysql mysqli

我有以下代码:

try
{
    if(!($stmt = $conn["DB"]->prepare('CALL `central`.`permissions_edit`(?,?,?,?,?);')))
    {
        $rtn["Errors"][] = "permissions_edit Prepare failed: (" . $conn["DB"]->errno . ") " . $conn["DB"]->error;
    }
    else{
        foreach ($data as $user => $areas) {
            foreach ($areas as $area => $access) {
                foreach ($access as $acc => $active) {
                    if($active != "U")
                    {
                        if(!($stmt->bind_param(
                            'siiis',
                            $key,
                            $user,
                            $area,
                            $acc,
                            $active
                        )))
                        {
                            $rtn["Error"][] = "permissions_edit Bind failed: (" . $conn["DB"]->errno . ") " . $conn["DB"]->error;
                        }
                        else if(!($stmt->execute()))
                        {
                            $rtn["Errors"][] = "permissions_edit Execute failed: (" . $conn["DB"]->errno . ") " . $conn["DB"]->error;
                        }
                        else if($res = $stmt->get_result())
                        {
                            if ($row = $res->fetch_assoc())
                            {

                                if(isset($row["Success"]) && $row["Success"] )
                                {
                                    $rtn["Results"][]= "user=$user areas=$area access=$acc active=$active Message=" . $row["Msg"];
                                }
                                else
                                {
                                    $rtn["Success"] = false;
                                    $rtn["Errors"][]= "user=$user areas=$area access=$acc active=$active Message=" . $row["Msg"];                   
                                }
                            }

                        }
                        else
                        {
                            $rtn["Errors"][] = "permissions_edit Get failed: (" . $conn["DB"]->errno . ") " . $conn["DB"]->error;
                        }

                    }
                    else
                        $rtn["Results"][]= "user=$user areas=$area access=$acc active=$active Message=Unchanged";
                }
            }
        }
    }


}
catch(Exception $e)
{
    $rtn["Errors"][] = "permissions_edit Error(" . $conn["DB"]->errno . "): " . $conn["DB"]->error;
}

使用此代码,其中循环重复1-2次完美 但是使用这个循环重复两次以上的代码会导致POST到PHP终止而没有响应

并在MySql日志中生成以下内容,

  

2017-03-03T16:47:49.284972Z 450 [注意]中止连接450到db:   'central'用户:'用户名'主机:'localhost'(读取时出错)   通讯包)

     

2017-03-03T16:47:49.582165Z 451 [注意]已中止   连接451到db:'central'用户:'username'主机:'localhost'   (读取通信包时出错)

所以问题是我做错了什么?

注意 我尝试添加

$res->free();
$stmt->close();

然后导致了

  

数据包无序。预计1收到7.数据包大小= 7

     

mysqli_stmt :: execute():MySQL服务器已经消失

     

mysqli_stmt :: execute():读取结果集的标题

时出错

来自info.php

PHP Version 5.6.27

mysqli
MysqlI Support  enabled
Client API library version  mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $
Active Persistent Links     0
Inactive Persistent Links   0
Active Links    0
Directive   Local Value Master Value
mysqli.allow_local_infile   On  On
mysqli.allow_persistent On  On
mysqli.default_host no value    no value
mysqli.default_port 3306    3306
mysqli.default_pw   no value    no value
mysqli.default_socket   no value    no value
mysqli.default_user no value    no value
mysqli.max_links    Unlimited   Unlimited
mysqli.max_persistent   Unlimited   Unlimited
mysqli.reconnect    Off Off
mysqli.rollback_on_cached_plink Off Off

2 个答案:

答案 0 :(得分:0)

问题是您需要致电[defaults setBool:YES forKey:@"notificationIsActive"]; [defaults synchronize]; UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = 0; //Enter the time here in seconds. localNotification.alertBody= @"Customer Saved to Database"; localNotification.timeZone = [NSTimeZone defaultTimeZone]; //localNotification.repeatInterval= NSCalendarUnitDay;//NSCalendarUnitMinute; //Repeating instructions here. localNotification.soundName= UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

这是一个有效的整洁版本

$conn["DB"]->next_result();

答案 1 :(得分:-2)

您可以看到来自https://dev.mysql.com/doc/refman/5.7/en/communication-errors.html

的错误
  

如果客户端成功连接但后来断开连接不正确或   终止后,服务器会递增Aborted_clients状态   变量,并将中止的连接消息记录到错误日志中。该   原因可以是以下任何一种:

     
      
  • 客户端程序在退出之前没有调用 mysql_close()

  •   
  • 客户的睡眠时间超过 wait_timeout 或    interactive_timeout 秒,不向网站发出任何请求   服务器

  •   
  • 客户端程序在数据传输过程中突然结束。

  •   

但是,我认为在循环中使用 bind_param 并不是一种好的做法,因为它应该在循环之前只使用一次。 第二个通知是确保即使出现错误也要关闭连接。

相关问题