Andrew
Andrew

Reputation: 82

Putting MYSQL table info into html table

I'm making a guestbook, and I have a problem, that when I want my info from my mysql table to be placed into a table it only creates an empty table.

Code's here:

mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Vardas</td>
<td width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Komentaras</td>
<td valign="top">:</td>
<td><? echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top"> Laikas </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<?php }
mysql_close();
?>

Upvotes: 0

Views: 123

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

If short tags aren't ON, <? echo $rows['id']; ?> will fail.

Either turn them on, or change to <?php echo $rows['id']; ?> and do the same for the others.


Footnotes

mysql_* functions deprecation notice:

http://www.php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.

Documentation for MySQL can be found at » http://dev.mysql.com/doc/.

Upvotes: 5

H&#252;seyin BABAL
H&#252;seyin BABAL

Reputation: 15550

Update your code like;

while($rows=mysql_fetch_array($result)){

to

while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){

You need to use associative array. Also this library is deprecated. Use Mysqli or PDO

Upvotes: 0

Related Questions