Page 1 of 1

Problem with nulls returned in an if statement

Posted: Mon Dec 14, 2009 5:06 pm
by craigwg
Hi,

I am trying to allow my users to comment on my blog. In English I want users to click "Leave a Comment" and see the entry along with existing comments. If there are NO comments I want to display a sentence that reads "Be the first one to leave a comment." It is the if statement that is killing me. Here is my code (part of it anyway! The section in question at least!):

Code: Select all

 
...
 
$query = "
SELECT c.VisitorName, date_format(c.DateOfComment, '%M %e, %Y' ) AS dateofcomment, c.Comment
FROM tblJournal AS j
LEFT JOIN tblJournalComments AS c on j.id = c.entryid
where c.approved=1 and j.id='".$id."'
";
$result = @mysql_query ($query); // Run the query.
 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    
    if (empty($result)) { //If the query above is blank...
    echo '<tr>
                <td align="right" colspan="2"><hr width="80%"></td>
        </tr>
        <tr>
                <td><h2>Be the first to leave a comment!  Use the form below.</h2></td>
        </tr>';
    } else { 
        echo '<tr>
                <td align="right" colspan="2"><hr width="80%"></td>
            </tr>
            <tr>
                <td align="left" valign="top" width="200"><b>           
                ' . $row['VisitorName'].'</b>&nbsp;said:<p></td>
                <td align="left">
                ' . $row['Comment'].'</p><font size="2" color="#666666">Posted on:
                ' . $row['dateofcomment'].'</font>
                </a></td>
            </tr>';
    }
}
...
 
On entries that have no comments, when I run that query it pulls back a single row, all fields marked NULL. So is that a row or not?
I have tried a number of things to check in the if statement, like isset, exists, etc. This one is the closest. This actually works, except it never repeats the text "Be the first to leave a comment. Use the form below." I think its because I am testing for the row to be empty, and it isn't empty...its a single row of nulls. I am thinking about putting an OR statement in the if clause, but not sure what to put there.

Any ideas?

You can see the page here if it helps:

http://www.craiggreenwood.com/journalcomment.php?id=183 (this has no comments)
http://www.craiggreenwood.com/journalcomment.php?id=180 (this has 3 comments)

Thanks,
Craig