Page 1 of 1

help please, cant fetch data from mysql

Posted: Fri Oct 16, 2009 1:37 pm
by strongbad
this is a page which displays all the questions in the DB
please tell me why cant i see the no data in the table that is produced...only empty cells

thanks :) (sorry for my language, not a native speaker)

Code: Select all

 
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
//order by descending order(of id)
$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 rows
?> <? echo $rows['id']; ?>
  <tr>
   <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
   <td bgcolor="#FFFFFF"><a href=view_topic.php?id=<? echo $rows['id']; ?> > <? echo $rows['topic']; ?></a></td>
   <td bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
   <td bgcolor="#FFFFFF"><? echo $rows['reply'];?></td>
   <td 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>
 

Re: help please, cant fetch data from mysql

Posted: Fri Oct 16, 2009 1:54 pm
by John Cartwright
Firstly, are you sure the query is returning rows?

Code: Select all

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
//order by descending order(of id)
$result = mysql_query($sql);
 
echo 'Query: '. $sql .'<br />';
echo 'Rows: '. mysql_num_rows($result) .'<br />';
 
Secondly, everything else looks o-k besides an errand echo in your loop. Try removing the bolded text below

while($rows=mysql_fetch_array($result)){ //start looping table rows
?> <? echo $rows['id']; ?>

Re: help please, cant fetch data from mysql

Posted: Fri Oct 16, 2009 2:12 pm
by strongbad
thanks
yeah i'm sure it's ok, i've added the lines you wrote and deleted the errand echo
and still, the page i get is a three empty rows of cells

i added a screen capture of the html that was produced

and this is the fixed code(with your testing lines) :

Code: Select all

 
<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "forum";
$tbl_name = "forum_question";
 
//connect to server and select database
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";
//order by descending order(of id)
$result = mysql_query($sql);
 
 
echo 'Query: '. $sql .'<br />';
echo 'Rows: '. mysql_num_rows($result) .'<br />';
?>
 
 
<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 rows
?> 
  <tr>
   <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
   <td bgcolor="#FFFFFF"><a href=view_topic.php?id=<? echo $rows['id']; ?> > <? echo $rows['topic']; ?></a></td>
   <td bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
   <td bgcolor="#FFFFFF"><? echo $rows['reply'];?></td>
   <td 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>
   
   
   
 
 

Re: help please, cant fetch data from mysql

Posted: Fri Oct 16, 2009 2:55 pm
by John Cartwright
Try changing all the instances of "<?" to "<?php" ... as you probably have short tags disabled. This is why it's good practice to always use the elongated tag :)

Re: help please, cant fetch data from mysql

Posted: Fri Oct 16, 2009 3:20 pm
by strongbad
Great!:)
thanks alot..i didn't know that you need to enable shortened <?php tags
i guess i'll keep on writing them long , its safer that way