IF statement works but ELSE doesn't?
I have the following statement and the IF ELSE doesn't seem to be working? By not working I mean its just running the while loop and display the H1 without checking to see if results isn't null?
Can anybody see any obvious errors?
if($interests_result != 0) {
print "<h1>Users with the same interests!</h1><div style='clear:both;'>";
while($interests_row = mysql_fetch_array($interests_result, MYSQL_ASSOC))
{
$int_user = $interests_row['user_id'];
$int_twit_user = $interests_row['twitterUser'];
$divLeft = '<div class="user-box" id="thediv_'.$int_user.'"><div class="twithandlepic"><img src="http://api.twitter.com/1/users/profile_image/';
$divRight = '<div class="twithandle">';
$clearDiv = '<div style="clear:both;"></div>';
print $divLeft.strip_tags($int_twit_user)."?size=normal\" style=\"width:48px; height:48px;\"/><br \/>".$int_twit_user.$divRight."<a href='javascript:void(0);' id='".$interests_row['user_id']开发者_JAVA百科."' class='getIntPoint'>Get ".$interests_row['coff']." <input type='hidden' value='".$interests_row['coff']."' class='credoff' name='credoff'/> credit(s)</a><br /></div>$clearDiv</div></div>";
}
print $clearDiv."</div>";
}
else
{
print "No users to display!";
}
The value assigned to interests_result is...
$interests_query = "SELECT * FROM produgg_users
join user_interests on produgg_users.id = user_interests.user_id
where (interest = '$interest1' OR interest = '$interest2' OR interest = '$interest3') and produgg_users.id != '".$usersClass->userID()."' and credits >= coff and produgg_users.id NOT IN (select concat_ws(',', followedID) from produgg_activity where followerID = '".$usersClass->userID()."') ORDER BY coff DESC LIMIT 30;";
$interests_result = mysql_query($interests_query) or die(mysql_error());
$interests_result
is a mysql resource. It is not equal to 0, ($interests_result != 0
is true), therefore that block of the if
is being executed.
What you want to be checking instead is this:
if (mysql_num_rows($interests_result) != 0) {
mysql_query()
returns a resource on success, or FALSE on error, not 0
mysql_query
returns either a resource or FALSE
(if there is an error).
You appear to expect it to contain the number of rows returned, which it doesn't.
Use mysql_num_rows for your comparison.
You want to check for the number of rows returned.
精彩评论