shona
shona

Reputation: 1

What is the problem with this php code?

This the simple php code I am using to view contents of a forum. The problem is , it's working fine in one of my laptop but in second it do not show the output.

  // Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

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

I checked everything else and they seems fine. Data is present in the database but the it's not showing in forum. Can anybody help me in this? operating sys : win7

Upvotes: 0

Views: 131

Answers (2)

Gaurav
Gaurav

Reputation: 28755

you should use

<?php echo $rows['id']; ?> instead of <? echo $rows['id']; ?>

short tag may be disabled.

Upvotes: 2

Your Common Sense
Your Common Sense

Reputation: 157880

time to learn debugging, pal.

Good place to start: http://www.ibm.com/developerworks/library/os-debug/

for mysql it's extremely handy to run queries this way:

$result=mysql_query($sql) or trigger_error(mysql_error()." ".$sql);

and make sure you can see all errors occurred. if you can't as a quick fix you can add these lines at the top of the script

ini_set('display_errors',1);
error_reporting(E_ALL);

but for the production state display_errors value should be changed to 0

It is also good practice to check HTML source instead of watching rendered page in the browser. It will tell you if you have any problem with short tags or not. It's always good to know if you have any problem before going to fix it.

Upvotes: 2

Related Questions