PHP returning [false] when I have not asked it too-Collection of common programming errors
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?
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:
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)) { $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.to_userid = info.id AND friend.from_userid = '{$this->userid}' AND friend.approved = 1 ORDER BY astatus.onlinestatus DESC"); if (mysql_num_rows($friendlook)) { $row = mysql_fetch_assoc($friendlook); $rows[] = $row; print json_encode($rows); } else { $arr = array("error" => "Your friends are not online"); print json_encode($arr); } return; } $arr = array("error" => "No Friends?
Search above for New users or invite some friends."); print json_encode($arr); } -
http://php.net/manual/en/function.mysql-fetch-assoc.php
Return Values: Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.”
Your select statement returns
0
rows,mysql_fetch_assoc
returnsfalse
, you insert that false value into$rows
, so you get an array containing a single false value.Use
if($row === false)
orif($row)
to detect if you received a valid row. -
Not exactly an answer to your question, but maybe you want to use something like
public function friendsOutputAsJSON($userid) { $this->userid = $userid; $result = 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.to_userid = info.id AND friend.from_userid = '". mysql_real_escape_string($userid). "' AND friend.approved = 1 ORDER BY astatus.onlinestatus DESC "); if ( !$result ) { yourErrorHandler(); } $rows = array(); while( $row=mysql_fetch_assoc($result) ) { $rows[] = $row; } if( empty($rows) ) { $rows = array("error" => "No Friends?
Search above for New users or invite some friends."); } print json_encode($arr); }i.e.
- not performing an extra query to check if there are any records (which should use Count(*) if you want to keep it)
- fetch all records, instead of only the first
- checking if some records have been fetched instead using mysql_num_rows
- having only one return/print “exit point” for the function
- (possibly preventing sql injections – depends on what $userid can be/how it is checked before calling the function)
-
i think json_encode() fails .
i advice you to check the error type :
switch (json_last_error()) { case JSON_ERROR_NONE: echo ' - No errors'; break; case JSON_ERROR_DEPTH: echo ' - Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: echo ' - Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: echo ' - Unexpected control character found'; break; case JSON_ERROR_SYNTAX: echo ' - Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: echo ' - Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: echo ' - Unknown error'; break; }
-
To avoid printing the false, use a
if (mysql_num_rows($friendlook))
(in the $friendlook part) to check whether there are results before trying to print anything.If there are no results, nothing will be printed. and you won’t have that annoying issue.
Performance note: Don’t use print, use echo instead.
Originally posted 2013-11-10 00:11:15.