Search code examples
phpmysqlhtml-tableforum

Can't view the content of my main forum table in PHP


I am trying to view forum topics in this table. However the content of the table will not display. I don't know what I am missing from my code. It shows the table and displays the correct amount of rows but no text.

<?php
include "db.php" ?>

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending

$result=mysql_query($sql);
?>


<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>

<?php


// Start looping table row
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>

<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"><strong>Create New Topic</strong> </a></td>
</tr>
</table>

Solution

  • It's probably because your server does not have short_open_tag configuration enabled.

    Because of that, the following line is not parsed as PHP code, but rather it's considered as plain text.

    <? echo $rows['view']; ?>
    

    Use this instead:

    <?php echo $rows['view']; ?>
    

    Short tags are considered a PITA in the coding world, because if you ever have to migrate your code to a server that doesn't support it, or you can't enable it, than you have to edit all your code just for that environment when you could have just gone with normal tags. However, as of 5.4.x, short hand tags are enabled by default and parsed in any PHP document. But it's good to keep in mind different environments.