skip to Main Content

I have a bdd MySQL which is regularly updated with a text generated by an artificial intelligence. I want to show this infinite text as a web publishing so we did this, as a rough test:

<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>

Which is calling 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>');

And we got two problems. First, we only get a blank page. Second, after one or two seconds, we got this error message:

{Request error : "readyState":4,"responseText":"","status":500,"statusText":"Internal Server Error"}

Any ideas?

my Error log

> [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

Following your advices I have now :

<!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>

And as for 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>');
?> 

It’s working ! With this code, the page is refreshing every X seconds but it’s doesn’t seems to be smooth at all. If I set it up for refreshing every 100ms, it work and stutter like with F5. Do you know how to fix it ? How to set up a smooth refresh ? Is there a more interesting way to use ajax here ?

2

Answers


  1. Your connection should not have the quotes in…

    $dbhandle = new PDO("mysql:host='localhost';dbname='writing'", "root", "*********");  //this connects to your mysql table
    

    Should be…

    $dbhandle = new PDO("mysql:host=localhost;dbname=writing", "root", "*********");  //this connects to your mysql table
    
    Login or Signup to reply.
  2. There are some mistakes in your code look at mine below:

    <?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>';
    

    By the way look at the style of the echo s i wrote, This is called echo multiple parameters which is a better way than concatenating with .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search