I am not sure why, But when I run the following code and there is nothing in the database I get the error [false] when I view the PHP page instead of the correct error message
public function fetchfriends($userid)
{
$this->userid = $userid;
$contact_check = mysql_query("
SELECT *
FROM user_contact
WHERE from_userid = '{$this->userid}'
AND approved != 1
");
if(!mysql_num_rows($contact_check))
{
$arr = array("error" => "No Friends?<br/>Search above for New users or invite some friends.");
print json_encode($arr);
}
else
{
$friendlook = mysql_query("
SELECT friend.to_userid,
info.username,
info.firstname,
info.lastname,
info.status,
astatus.onlinestatus
FROM user_contact AS friend
LEFT JOIN user_info AS info ON friend.to_userid = info.id
LEFT JOIN user_online AS astatus ON friend.to_userid = astatus.userid
WHERE friend.from_userid = '{$this->userid}'
AND friend.approved = 1
AND friend.to_userid = info.id
ORDER BY astatus.onlinestatus DESC
");
$row = mysql_fetch_assoc($friendlook);
$rows[] = $row;
print json_encode($rows);
}
}
I was wondering if anyone would know why?
You have 2 SQL queries and you only check if the first has any results. The problem is that if the first has results and the second doesn’t you will not get your error message, but the
[false]you receive now.Your code should look something like: