I have a SQL table and I would like to display it in a HTML page. My code is displaying a table but the content is empty although my table is filled with data.
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$database = 'karin';
$connect = mysql_connect('localhost','root', 'karin');
if (!$connect) {
die(mysql_error());
}
if (!mysql_select_db($database))
die("Can't select database");
$results = mysql_query("SELECT * FROM service_messages");
if (!$results) {
die("Query to show fields from table failed");
}
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['partner']?></td>
<td><?php echo $row['GUID']?></td>
<td><?php echo $row['serviceName']?></td>
<td><?php echo $row['serviceID']?></td>
<td><?php echo $row['countries']?></td>
<td><?php echo $row['rate']?></td>
<td><?php echo $row['ips']?></td>
<td><?php echo $row['callback']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
Why doesn't it show the table content? Thanks!
Try this
while($row = mysql_fetch_row($results)) {
?>
<tr>
<td><?php echo $row['partner']?></td>
<td><?php echo $row['GUID']?></td>
<td><?php echo $row['serviceName']?></td>
<td><?php echo $row['serviceID']?></td>
<td><?php echo $row['countries']?></td>
<td><?php echo $row['rate']?></td>
<td><?php echo $row['ips']?></td>
<td><?php echo $row['callback']?></td>
</tr>
<?php
}
?>
For more refrence : http://php.net/manual/en/function.mysql-fetch-row.php
1). Don't use mysql_*. Please use PDO or MySQLi.
2). I don't find any <table>
(table starting tag) anywhere in your code.
3). Use mysql_fetch_assoc
instead of mysql_fetch_array
.
Here's the updated code.
$database = 'karin';
$connect = mysql_connect('localhost','root', 'karin');
if (!$connect) {
die(mysql_error());
}
if (!mysql_select_db($database))
die("Can't select database");
$results = mysql_query("SELECT * FROM service_messages");
if (!$results) {
die("Query to show fields from table failed");
}
?>
<table>
<?php
while($row = mysql_fetch_assoc($results)) {
?>
<tr>
<td><?php echo $row['partner']?></td>
<td><?php echo $row['GUID']?></td>
<td><?php echo $row['serviceName']?></td>
<td><?php echo $row['serviceID']?></td>
<td><?php echo $row['countries']?></td>
<td><?php echo $row['rate']?></td>
<td><?php echo $row['ips']?></td>
<td><?php echo $row['callback']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Well, for some reason it works like this:
<?php
//ini_set('display_errors', 'On');
//error_reporting(E_ALL);
$mysqli = new mysqli("localhost", "root", "2014", "cd");
$q = $mysqli->query("select * from service_messages");
while($row = $q->fetch_assoc()){
$arr[] = $row;
}
echo json_encode($arr);
?>