我有一个基于民意调查的聊天应用程序。服务器看起来像这样:
<?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
答案 0 :(得分:0)
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
);
}
嗯,我明白了
dibi :: query(&#39; SELECT id
,time
,unsafe_from
,unsafe_messageText
FROM messages
WHERE id&gt;&#39;。$ lastDownloadedId) - &GT;使用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
);
}
答案 1 :(得分:0)
问题在于在文件开头打开mysql连接,但稍后再使用它。我在localhost上修复了它,但忘了把它上传到服务器。