我有一个bdd MySQL,它定期更新由人工智能生成的文本。我想将此无限文本显示为网络发布,所以我们这样做,作为一个粗略的测试:
<head>
<script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<div id="myTable"></div>
<script type="text/javascript">
function page_refresh() {
$.ajax({
url: 'getData.php',
type: 'GET',
success: function(data) {
$('#myTable').html(data);
},
error: function(request,error) {
alert("Request error: " + JSON.stringify(request));
}
});
}
var period = 10000; // NOTE: period is passed in milliseconds
setInterval(page_refresh, period);
</script>
</body>
正在调用getData.php
<?php
$dbhandle = new PDO("mysql:host='localhost';dbname='writing'", "root", "*********"); //this connects to your mysql table
$sql = "SELECT text, id, date FROM table_02 ;"; //this is your query, where you select what data you want from the table
$query = $dbhandle->prepare($sql);
if ($query->execute() == FALSE) {
die("Query error: " . implode($query->errorInfo(), ' '));
} //this is a measure to close the connection if there is an error
echo('<table>');
echo('<tr><th>id</th><th>date</th><th>text</th></tr>');
while ($row = $query->fetch()) {
echo('<tr>');
echo('<td>').$row ['id'].'</td>');
echo('<td>').$row ['date'].'</td>');
echo('<td>').$row ['text'].'</td>');
echo('</tr>');
}
echo('</table>');
我们遇到了两个问题。首先,我们只得到一个空白页面。其次,在一两秒钟之后,我们收到了以下错误消息:
{Request error : "readyState":4,"responseText":"","status":500,"statusText":"Internal Server Error"}
有什么想法吗?
我的错误日志
> [Thu Jun 22 06:25:01.645901 2017] [mpm_prefork:notice] [pid 17562]
> AH00163: Apache/2.4.18 (Ubuntu) configured -- resuming normal
> operations [Thu Jun 22 06:25:01.645918 2017] [core:notice] [pid 17562]
> AH00094: Command line: '/usr/sbin/apache2' [Thu Jun 22 08:12:32.453382
> 2017] [:error] [pid 19559] [client 77.198.111.62:57256] PHP Parse
> error: syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 08:21:09.579406 2017]
> [:error] [pid 19560] [client 77.198.111.62:57430] PHP Parse error:
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 08:21:09.582093 2017]
> [:error] [pid 19648] [client 77.198.111.62:57431] PHP Parse error:
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 09:09:04.947344 2017]
> [:error] [pid 19559] [client 77.198.111.62:59193] PHP Parse error:
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 09:09:11.454622 2017]
> [:error] [pid 19648] [client 77.198.111.62:59196] PHP Parse error:
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14 [Thu Jun 22 09:10:28.529637 2017]
> [:error] [pid 19561] [client 77.198.111.62:59227] PHP Parse error:
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14 [Thu Jun 22 09:10:32.709239 2017]
> [:error] [pid 19558] [client 77.198.111.62:59229] PHP Parse error:
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14 [Thu Jun 22 09:11:39.122539 2017]
> [:error] [pid 19559] [client 77.198.111.62:59270] PHP Parse error:
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 13, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 09:11:51.476161 2017]
> [:error] [pid 19648] [client 77.198.111.62:59285] PHP Parse error:
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 13, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 09:14:14.697094 2017]
> [:error] [pid 19557] [client 77.198.111.62:59440] PHP Parse error:
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 13, referer:
> http://82.223.18.239/testTom.php
根据您的建议我现在:
<!DOCTYPE html>
<head>
<script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<div id="myTable"></div>
<script type="text/javascript">
function page_refresh() {
$.ajax({
url : 'getData.php',
type : 'GET',
success : function(data) {
$('#myTable').html(data);
},
error : function(request,error)
{
alert("Request error : "+JSON.stringify(request));
}
});
}
var period = 10000; //NOTE: period is passed in milliseconds
setInterval(page_refresh, period);
</script>
</body>
至于getData.php:
<?php
$dbhandle = new PDO("mysql:host=localhost;dbname=writing", "root", "*********");//this connects to your mysql table
$sql = "SELECT text, id, date FROM table_02 ;"; //thise is your query, where you select what data you want from the table
$query = $dbhandle->prepare($sql);
if ($query->execute() == FALSE)
{ die("Query error: " . implode($query->errorInfo(), ' ')); } //this is a measure to close the connection if there is an error
echo('<table>');
echo('<tr><th>le nom de la rose</th></tr>');
while ($row = $query->fetch()) {
echo('<tr>');
##echo('<td>'.$row ['id'].'</td>');
##echo('<td>'.$row ['date'].'</td>');
echo('<td>'.$row ['text'].'</td>');
echo('</tr>');
}
echo('</table>');
?>
它正在运作!使用此代码,页面每X秒刷新一次,但它看起来并不平滑。如果我将它设置为每100毫秒刷新一次,它就像F5一样工作和口吃。你知道怎么解决吗?如何设置顺利刷新?有没有更有趣的方法在这里使用ajax?
答案 0 :(得分:2)
您的连接不应该包含引号......
$dbhandle = new PDO("mysql:host='localhost';dbname='writing'", "root", "*********"); //this connects to your mysql table
应该......
$dbhandle = new PDO("mysql:host=localhost;dbname=writing", "root", "*********"); //this connects to your mysql table
答案 1 :(得分:1)
您的代码中存在一些错误,如下所示:
<?php
$dbhandle = new PDO("mysql:host=localhost;dbname=writing", "root", "*********"); //this connects to your mysql table
$sql = "SELECT text, id, date FROM table_02 ;"; //this is your query, where you select what data you want from the table
$query = $dbhandle->prepare($sql);
if ($query->execute() == FALSE) {
die("Query error: " . implode(' ', $query->errorInfo()));
} //this is a measure to close the connection if there is an error
echo '<table>';
echo '<tr><th>id</th><th>date</th><th>text</th></tr>';
while ($row = $query->fetch()) {
echo '<tr>';
echo '<td>', $row ['id'], '</td>';
echo '<td>', $row ['date'], '</td>';
echo '<td>', $row ['text'], '</td>';
echo '</tr>';
}
echo '</table>';
顺便看一下我写的echo的样式,这叫做echo multiple parameters,这比用.