PHP table isn't displaying


 
Thread Tools Search this Thread
Top Forums Web Development PHP table isn't displaying
# 1  
Old 02-06-2015
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

Code:
<?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

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

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

# 2  
Old 02-06-2015
I'd use the echo "</table>" outside of the while loop.

Quote:
I have tired to set up a long-polling AJAX script to poll my PHP script every second.
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
# 3  
Old 02-07-2015
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
# 4  
Old 02-08-2015
I didnt say 'remove' i said 'outside the loop'.

OLD:
Code:
        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:
Code:
        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:
Code:
$sql = "SELECT * from username ORDER BY id DESC LIMIT 1";

Code:
$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...

Last edited by sea; 02-08-2015 at 10:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Web Development

Getting Rid of Annoying Bootstrap Table Borders and Wayward Table Lines

Bootstrap is great; but we have had some issues with Bootstrapped <tables> (and legacy <fieldset> elements) showing annoying, wayward lines. I solved that problem today with this simple jQuery in the footer: <script> $(function(){ $('tr, td, fieldset,... (0 Replies)
Discussion started by: Neo
0 Replies

2. Programming

Table Duplication in PHP

Hey, I am making a Facebook like Page system as my first project, So far it's been bate in mind I did it from my 3DS at the same time as my PC gets replaced, So far it's turned out great. Now I am on to creation the blocking system I need to get the code to say If the user already likes the... (0 Replies)
Discussion started by: AimyThomas
0 Replies

3. Shell Programming and Scripting

how to convert a shell script to a php script for displaying next word after pattern match

I have a shell script which I made with the help of this forum #!/bin/sh RuleNum=$1 cat bw_rules | sed 's/^.*-x //' | awk -v var=$RuleNum '$1==var {for(i=1;i<=NF;i++) {if($i=="-bwout") print $(i+3),$(i+1)}}' Basically I have a pages after pages of bandwidth rules and the script gives... (0 Replies)
Discussion started by: sb245
0 Replies

4. Homework & Coursework Questions

Displaying text from a MySQL table which can be modified

Hi am creating a website for my third year at uni, am trying to create a website where the client can update the content of the site themselves, i will have a news page and i want the content to be draw from my database and displayed on the front end of the site using php, i also want to have an... (1 Reply)
Discussion started by: richeyrich86
1 Replies

5. UNIX for Dummies Questions & Answers

Displaying text from a MySQL table which can be modified

Hi am creating a website for my third year at uni, am trying to create a website where the client can update the content of the site themselves, i will have a news page and i want the content to be draw from my database and displayed on the front end of the site i also want to have an admin side... (3 Replies)
Discussion started by: richeyrich86
3 Replies

6. Web Development

php script + not displaying webpage

Hi guys Im in the process of designing a website for my company which does the following 1) Daily execution a perl script. The perl script's output is diverted to a .html file. 2) The .html file generated from 1) sets up a centralised webpage showing options as radio buttons (the radio... (1 Reply)
Discussion started by: JamesGoh
1 Replies

7. Shell Programming and Scripting

PHP: Sorting HTML table

Hi All, I have an html table which looks like this: <table align="center" border="1"> <CAPTION><EM>Heading for Table</EM></CAPTION> <tr><td><b>1</b></TD><TD><b>2</b></TD><TD><b>3</b></TD><TD><b>4</b></TD><TD><b>TOTAL</b></TD><TD><b>DATE</b></td></tr> <tr><td>88088283</TD> <TD>87613101</TD>... (1 Reply)
Discussion started by: pondlife
1 Replies

8. Shell Programming and Scripting

displaying output in a table.

Hello, I've just finished my first script (about displaying open ports on the computer and who opened them) and everything is fine with it but I want to style it a little bit just for the sake of learning how to do this. What I want to do is make the display like the one of ps, for example,... (6 Replies)
Discussion started by: sanchopansa
6 Replies
Login or Register to Ask a Question