Reputation: 48
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?
Upvotes: 1
Views: 299
Reputation: 74217
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\">";
<?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\">";
?>
Upvotes: 1
Reputation: 22711
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">';
?>
Upvotes: 1