PHP table isn't displaying

I have a PHP script which connects to a MySQL database and should output a table in HTML. I have tired to set up a long-polling AJAX script to poll my PHP script every second. It seems to work based on what I can see in my browser debugger, however the table isn't showing on the page. Can anybody help me find what's wrong with my code?

reocrd.php


<?php
    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    flush();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
      <?php
        $servername = "localhost";
        $username = "recorduser";
        $password = "recorduser";
        $database = "record";

        // Create connection
        $conn = mysqli_connect($servername, $username, $password, $database);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }

        $sql = "SELECT * from username ORDER BY id DESC LIMIT 1";
        $result = mysqli_query($conn, $sql);

        echo "
        <table border='1'>
            <tr>
                <th>Firstname</th>
                <th>Lastname</th>
            </tr>";

        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['lastname'] . "</td>";
            echo "</tr>";
            echo "</table>";
        }
        mysqli_close($conn);
      ?>
    </body>
</html>

test3.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Comet php backend</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <head>
            <script type="text/javascript" src="http://code.jquery.com/jquery- 1.11.2.min.js"></script>
            <script type="text/javascript" src="client1.js"></script>
        </head>
        <h1>Response from server:</h1>
    </body>
</html>

client1.js

setInterval(function(){
    $.ajax({ 
        url      : "record.php", 
        success  : function(data){
            //Update your dashboard gauge
            salesGauge.setValue(data.value);
        }, 
        dataType : "json"
    });
}, 30000);

I'd use the echo "</table>" outside of the while loop.

Also, just for testing, i'd lower the file-creation-intervall from every to just every 10'th second.
This said, i'm confused by client.js, which actualy seems to be using an intervall of 30 secs?
Or am i misreading that part?

hth

Hi,

i have removed echo table out side the loop.
and lowered the interval second to 10.
but the code doesn't work.
can you fix my code.

thanks and regards

I didnt say 'remove' i said 'outside the loop'.

OLD:

        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['lastname'] . "</td>";
            echo "</tr>";
            echo "</table>";
        }
        mysqli_close($conn);

NEW:

        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['lastname'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        mysqli_close($conn);

Though...you should see at least one line now anyway...

Hope this helps

EDIT:
After rereading, i assume there is a missing dollar sign before the username:

$sql = "SELECT * from username ORDER BY id DESC LIMIT 1";
$sql = "SELECT * from $username ORDER BY id DESC LIMIT 1";

I'm a bit rusty on php, not sure if you need to 'split' the string to connect it with the variable...