1

I am trying to display a table onto a html page that contains users SQL data.

Assume I can connect and there is information in the table.

<?php
echo "<table>
            <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Hair Color</th>
        </tr>";
    $result = mysql_query("SELECT * FROM `table`");
    while($row = mysql_fetch_assoc($result))
    {
        echo "<tr>";
        echo "<td>" .$row['first']."</td>";
        echo "<td>".$row['last']."</td>";
        echo "<td>".$row['color']."</td>";
        echo "</tr>";
    }
    echo "</table>";

    echo "<input type="button" value="updateTable" id="btn">";
  ?>

Where am I going wrong because no data is being output?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Wyatt
  • 48
  • 8
  • 1
    Do you have any data in your database? – Sablefoste Nov 06 '13 at 18:57
  • Check your query is executing ? – Shafeeque Nov 06 '13 at 18:57
  • You shouldn't use mysql_query(), but if you are you need a database link. See http://php.net/manual/en/function.mysql-query.php – Sablefoste Nov 06 '13 at 18:59
  • As shown in an answer below, this line `echo "";` should be either `echo '';` or escaping your double quotes like this `echo "";` other than that, your code is fine and check to see if you do have data in your DB and that your columns are correctly named. Plus *"assuming"* you are connected to your DB. – Funk Forty Niner Nov 06 '13 at 19:13

2 Answers2

1

Can you try this,

Below line is causing the error,

 echo "<input type="button" value="updateTable" id="btn">";

should be something like this

 echo '<input type="button" value="updateTable" id="btn">';

PHP code:

  <?php     

        echo "<table>
                <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Hair Color</th>
            </tr>";
        $result = mysql_query("SELECT * FROM `table`");
        while($row = mysql_fetch_assoc($result))
        {
            echo "<tr>";
            echo "<td>" .$row['first']."</td>";
            echo "<td>".$row['last']."</td>";
            echo "<td>".$row['color']."</td>";
            echo "</tr>";
        }
        echo "</table>";

        echo '<input type="button" value="updateTable" id="btn">';          

?>
Krish R
  • 22,583
  • 7
  • 50
  • 59
  • Better yet would be to turn off the interpreter when outputting the hardcoded HTML. That way it still works without escaping if/when a `'` has to be embedded somewhere, and you get syntax highlighting on the HTML. – Izkata Nov 06 '13 at 19:24
1

Firstly, do consider switching over to MySQLi_ and/or PDO and prepared statements. MySQL_ is deprecated.

Do read these two articles (before going LIVE):

There was one line with a syntax error, this one:

echo "<input type="button" value="updateTable" id="btn">";

Which should either be written as:

echo '<input type="button" value="updateTable" id="btn">';

or:

echo "<input type='button' value='updateTable' id='btn'>";

or escaping the double quotes:

echo "<input type=\"button\" value=\"updateTable\" id=\"btn\">";

Tested on my own server:

<?php
echo "<table>
    <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Hair Color</th>
</tr>";

// CONNECT TO THE DATABASE

$db = mysql_connect("localhost", "username", "password");
if($db->connect_errno > 0) {
  die('Connection failed [' . $db->connect_error . ']');
}

$mydb = "your_db";
$table = "your_table";
mysql_select_db($mydb) or die ('Unable to select database');

$result = mysql_query("SELECT * FROM `$table`");

while($row = mysql_fetch_assoc($result)) {
        echo "<tr>";
        echo "<td>" .$row['first']."</td>";
        echo "<td>".$row['last']."</td>";
        echo "<td>".$row['color']."</td>";
        echo "</tr>";
    }
    echo "</table>";

    echo "<input type=\"button\" value=\"updateTable\" id=\"btn\">";
?>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141