【问题标题】:mysql server has gone away in result of storing API response in phpmysql 服务器因在 php 中存储 API 响应而消失了
【发布时间】:2012-04-03 11:55:32
【问题描述】:

我正在经历mysql服务器已经消失,以响应通过php代码将API结果存储在mysql数据库中,

$api_response = sendmsg($group_detail['cell_no'], $SMS); //function to execute API

    //update sms sent record
$sql = "INSERT INTO sms_sent_record (record_id, api_response) 
                         VALUES (NULL, '$api_response')";
$result = mysql_query($sql) or die('Error, updating sms & group count               
              in sms_sent_count failed : ' . mysql_error()); 

我尝试了许多其他选项,例如将 api 响应存储在数组中,然后将其存储在 mysql 表中,但无论如何它都会引发相同的错误。此错误后函数中的其他代码将不会被服务器执行。

我认为这是由于 api 响应延迟和 mysql 中的超时问题。

有什么办法可以避免这个错误并以正确的方式存储结果。

【问题讨论】:

  • "The MYSQL server has gone away" 是一个实际错误。可能是最无用的一个,但仍然是一个错误。

标签: php mysql


【解决方案1】:

如果你省略 sendmsg 并执行以下操作,它是否有效:

$api_response = 'test';//sendmsg($group_detail['cell_no'], $SMS); //function to execute API 

如果是这样,sendmsg() 是否随时使用某种 mysql 连接? 如果是这样,您可能希望存储第一次连接到数据库时的链接并重复使用它。

$link=mysql_connect();
..
..
mysql_query($sql,$link);

【讨论】:

  • 它有效。但是 sql 只记录前两个响应......其余的代码根本不运行。
  • $api_response 通常是什么样的? -您可能还想使用 '$api_response=mysql_real_escape_string($api_response);' 来转义它
【解决方案2】:

There are lots of reasons for the gone away message 但我怀疑您的问题是您在 API 调用(发送短信?)之前打开了与数据库的连接,并且 API 调用导致服务器在 INSERT 运行之前终止您的连接

尝试将打开的数据库连接移至 INSERT 语句之前:

$api_response = sendmsg($group_detail['cell_no'], $SMS); //function to execute API
// open connection here
$sql = "INSERT INTO sms_sent_record (record_id, api_response) 
                         VALUES (NULL, '$api_response')";
$result = mysql_query($sql) or die('Error, updating sms & group count               
              in sms_sent_count failed : ' . mysql_error()); 

检查您为 wait_timeout 设置的值,这是

服务器等待活动的秒数 关闭之前的非交互式连接。

如果您将此值设置得非常低,则服务器将在打开连接和运行查询之间使您的连接超时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2015-04-14
    • 2012-05-25
    相关资源
    最近更新 更多