Page 1 of 1

mysql_fetch_array "removes" my html

Posted: Wed Dec 21, 2011 10:06 am
by Fonis
Hey guys

I'm kind of new to php and coding, but i am trying to create some thing like a forum. the problem for me is that in the page that shows all the topics (i'm calling it guides, cause it's supposed to be a site for guides), the topics won't show.

here is my code:

Code: Select all

<?php

//open database
$connect = mysql_connect("****","****","*****") or die("couldn't connect!");
mysql_select_db("******") or die("couldn't find db");

// Order result by descending 
$sql = "SELECT * FROM $guide ORDER BY id DESC";

$result=mysql_query($sql);


?>
<table width="900px" 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>guide</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)) {
?>

<tr>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><a href="view_guide.php?id=<?php echo $rows['id']; ?>"><?php echo $rows['guide']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['datetime']; ?></td>
</tr>

<?php
}
?>


<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_guide.php"><strong>Create New Guide</strong> </a></td>
</tr>
</table>
the stuff that won't show is the html that is surrounded by the mysql_fetch_array.
what did i do wrong, and how can i fix it?

Re: mysql_fetch_array "removes" my html

Posted: Wed Dec 21, 2011 10:31 am
by mikosiko
in the code that you posted $guide is undefined, therefore the rest of your code is failing

you should have php's error_reporting set to E_ALL and display_errors set to ON (temporarily) on your development environment so that any php detected errors will be reported and displayed to help point out problems like this one; adding this 2 lines at the beginning of your code ( after <php? ) will do that for this particular script:

Code: Select all

error_reporting(E_ALL); 
ini_set("display_errors", 1);
in addition, and also as a temporary solution (there are better ways to do the same) modify this line in your code:

Code: Select all

$result=mysql_query($sql);
to this

Code: Select all

$result=mysql_query($sql) or die('Error triggered ... Query was:  ' . $sql . '  Error was : ' .  mysql_error());

Re: mysql_fetch_array "removes" my html

Posted: Wed Dec 21, 2011 10:47 am
by Fonis
thanks for the reply, but the reason the $guide is undefined is because i didn't post all my database information here... (i think)

the only thing not showing up when i run the script is the "middle" part of the table, because it's surrounded by the mysql_fetch_array. if i delete the mysql_fetch_array, and leave the part of the table as it is, then the whole thing shows.
therefore, i need a way of showing the data of my database in the table.

btw, what does the script you've written do better than mine? i can't seem to figure it out, but of cause i'm also new to all this

Re: mysql_fetch_array "removes" my html

Posted: Wed Dec 21, 2011 10:52 am
by Fonis
okay, so i take it back again: there were something wrong with my $guide variable

thanks again