BZero Posted April 22, 2010 Share Posted April 22, 2010 (I tried posting this in an SQL forum first, but was told it was a PHP problem, so now I'm reposting it here. Sorry if it's the wrong place.) I made a very basic comment section on my test site with the following script, referring to an SQL table with the fields "name", "comment" and "id" (auto-increment), but when there was no data in the table (no comments) everything on the page, in and after this script, disappeared. <?php $con = mysql_connect("[host]","[username]","[password]"); @mysql_select_db("[database]") or die("Unable to select database"); if ($_POST[name]&&$_POST[comment]!=NULL) { mysql_query("INSERT INTO comments (name,comment) VALUES ('$_POST[name]','$_POST[comment]')"); } $query="SELECT * FROM comments ORDER BY id DESC"; $result=mysql_query($query) or die(mysql_error()); $num=mysql_num_rows($result) or die(mysql_error()); $i=0; $id=$num; while ($i < $num) { $name=mysql_result($result,$i,"name"); $comment=mysql_result($result,$i,"comment"); ?> <div class="comment"> <p><?php echo $id." ".$name; ?> says:</p> <p><?php echo $comment; ?></p> </div> <?php $id--; $i++; } if ($num < 1) { echo "Be the first to make a comment."; } ?> <h2>Reply</h2> <form action="index.html" method="post"> <p> <input type="text" name="name" size="22" /><small>Your name (required)</small> </p> <p> <textarea name="comment" cols="60" rows="10"></textarea> </p> <p> <input type="submit" value="Submit Comment" /> </p> </form> I tried removing parts of the script to see if I could find the source of the problem, and if I removed the line $num=mysql_num_rows($result) or die(mysql_error()); the bottom of the page reappeared, but of course, then my script wouldn't work at all. Like I said, the problem only occurs when there's no data in the table, so the number of rows would be 0, but why would that make everything on the page below this line disappear? The only way I've found to fix it, was to add a row of data, and change $i=0; $id=$num; while ($i < $num) { [...] if ($num < 1) { echo "Be the first to make a comment."; } to $i=0; $id=$num]-1; while ($i < $num-1) { [...] if ($num < 2) { echo "Be the first to make a comment."; } so the row I added wouldn't be displayed as a comment on the page. So it works, but since I'm trying to learn PHP and SQL, I'd like to know what causes this problem. Quote Link to comment https://forums.phpfreaks.com/topic/199379-why-does-this-make-the-bottom-of-my-page-disappear/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2010 Share Posted April 22, 2010 Just use $num=mysql_num_rows($result); Putting or die(mysql_error()) on mysql_num_rows() makes no sense because mysql_num_rows() does not set mysql_error() and it causes your code to die at that point when mysql_num_rows() returns a zero value. Quote Link to comment https://forums.phpfreaks.com/topic/199379-why-does-this-make-the-bottom-of-my-page-disappear/#findComment-1046398 Share on other sites More sharing options...
BZero Posted April 22, 2010 Author Share Posted April 22, 2010 Man, I can't believe I didn't try that. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/199379-why-does-this-make-the-bottom-of-my-page-disappear/#findComment-1046402 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.