Jump to content

Recommended Posts

(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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.