MySQL-服务器在长时间轮询时消失了


MySQL - Server has gone away while long Polling

我有一个基于长期民意调查的聊天应用程序。服务器看起来像这样:

<?php
header('Content-Type: application/json');
if (!isset($_GET['lastId'])) {
    exit(json_encode(array('status' => 'error_lastIdNotSet')));
}
//DIBI LIBRARY
//insert dibi lib code here, no space for it
//more about it here: http://www.dibiphp.com/cs/quick-start
$lastDownloadedId = intval($_GET['lastId']);
$counter = 290;
while ($counter > 0) {
    if ($lastDownloadedId !== intval(file_get_contents('./lastMessageId.txt'))) {
        $arr_output = array(
            'status' => 'new',
            'messages' => array()
        );
        if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {
            dibi::connect(array(
                'driver'   => 'mysql',
                'host'     => 'localhost',
                'username' => 'root',
                'password' => '',
                'database' => 'chat',
                'charset'  => 'utf8',
            ));
        } else {
            dibi::connect(array(
                'driver'   => 'mysql',
                'host'     => 'mysql.moxo.cz',
                'username' => 'u570204589_blog',
                'password' => '*************',
                'database' => 'u570204589_blog',
                'charset'  => 'utf8',
            ));
        }
        dibi::query('SET NAMES utf8');
        foreach(dibi::query('SELECT `id`, `time`, `unsafe_from`, `unsafe_messageText` FROM `messages` WHERE id>' . $lastDownloadedId)->fetchAll() as $row) {
            $arr_output['messages'][] = array(
                'id' => $row->id,
                'time' => $row->time->format('U'),
                'from' => $row->unsafe_from,
                'messageText' => $row->unsafe_messageText
            );
        }
        exit(json_encode($arr_output));
    }
    $counter--;
    usleep(100000);
}
echo json_encode(array('status' => 'timeout'));

如果在打开连接后的10秒内发送了新消息,则一切正常,但当稍后发送时,Mysql服务器已经消失,错误将被触发。

脚本当前位于http://anagmate.moxo.cz/projectsDir/chat/longPollServer.php.为了让它发挥作用?lastId=和最后一条消息id必须附加(可以在http://anagmate.moxo.cz/projectsDir/chat/lastMessageId.txt)。然后你可以试着给http://anagmate.moxo.cz/projectsDir/chat并回顾回应。

问题出在哪里?

错误如下:

Fatal error: Uncaught exception 'DibiDriverException' with message 'MySQL server has gone away' in /home/u570204589/public_html/projectsDir/chat/longPollServer.php:9
Stack trace:
#0 /home/u570204589/public_html/projectsDir/chat/longPollServer.php(9): DibiMySqlDriver->query('SELECT `id`, `t...')
#1 /home/u570204589/public_html/projectsDir/chat/longPollServer.php(9): DibiConnection->nativeQuery('SELECT `id`, `t...')
#2 /home/u570204589/public_html/projectsDir/chat/longPollServer.php(9): DibiConnection->query(Array)
#3 /home/u570204589/public_html/projectsDir/chat/longPollServer.php(42): dibi::query('SELECT `id`, `t...')
#4 {main}
SQL: SELECT `id`, `time`, `unsafe_from`, `unsafe_messageText` FROM `messages` WHERE id>55
  thrown in /home/u570204589/public_html/projectsDir/chat/longPollServer.php on line 9
foreach(dibi::query('SELECT `id`, `time`, `unsafe_from`, `unsafe_messageText` FROM `messages` WHERE id>' . $lastDownloadedId)->fetchAll() as $row) {
        $arr_output['messages'][] = array(
            'id' => $row->id,
            'time' => $row->time->format('U'),
            'from' => $row->unsafe_from,
            'messageText' => $row->unsafe_messageText
        );
    }

Emm,我明白吗dibi::query('SELECT idtimeunsafe_fromunsafe_messageText FROM messages WHERE id>'.$lastDownloadedId)->fetchAll()意味着您在每次迭代中都对数据库进行新的请求?

你能分享你的dibi课程吗?

尝试使用:

$result = dibi::query('SELECT `id`, `time`, `unsafe_from`, `unsafe_messageText` FROM `messages` WHERE id>' . $lastDownloadedId)->fetchAll();
foreach($result as $row) {
            $arr_output['messages'][] = array(
                'id' => $row->id,
                'time' => $row->time->format('U'),
                'from' => $row->unsafe_from,
                'messageText' => $row->unsafe_messageText
            );
        }

问题是在文件开始时打开mysql连接,但稍后使用。我在本地主机上修复了它,但忘记了将它上传到服务器。