{"id":1233,"date":"2022-08-30T15:14:36","date_gmt":"2022-08-30T15:14:36","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/php-returning-false-when-i-have-not-asked-it-too-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:36","modified_gmt":"2022-08-30T15:14:36","slug":"php-returning-false-when-i-have-not-asked-it-too-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/php-returning-false-when-i-have-not-asked-it-too-collection-of-common-programming-errors\/","title":{"rendered":"PHP returning [false] when I have not asked it too-Collection of common programming errors"},"content":{"rendered":"<p>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<\/p>\n<pre><code>public function fetchfriends($userid)\n{\n    $this-&gt;userid = $userid;\n    $contact_check = mysql_query(\"\n              SELECT *\n              FROM user_contact\n              WHERE from_userid = '{$this-&gt;userid}'\n              AND approved   != 1\n            \");\n\n\nif(!mysql_num_rows($contact_check))\n        {\n\n                $arr = array(\"error\" =&gt; \"No Friends?<br \/>Search above for New users or invite some friends.\");\n                print json_encode($arr);\n        }\n        else\n        {\n                $friendlook = mysql_query(\"\n                    SELECT friend.to_userid,\n                           info.username,\n                           info.firstname,\n                           info.lastname,\n                           info.status,\n                           astatus.onlinestatus\n\n                    FROM      user_contact AS friend\n                    LEFT JOIN user_info    AS info    ON friend.to_userid = info.id\n                    LEFT JOIN user_online  AS astatus ON friend.to_userid = astatus.userid\n\n                    WHERE friend.from_userid = '{$this-&gt;userid}'\n                      AND friend.approved    = 1\n                      AND friend.to_userid   = info.id\n\n                    ORDER BY astatus.onlinestatus DESC\n                \");\n\n                $row = mysql_fetch_assoc($friendlook);\n                $rows[] = $row;\n                print json_encode($rows);\n        }\n    }\n<\/code><\/pre>\n<p>I was wondering if anyone would know why?<\/p>\n<ol>\n<li>\n<p>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&#8217;t you will not get your error message, but the <code>[false]<\/code> you receive now.<\/p>\n<p>Your code should look something like:<\/p>\n<pre><code>public function fetchfriends($userid)\n{\n    $this-&gt;userid = $userid;\n    $contact_check = mysql_query(\"SELECT * FROM user_contact WHERE from_userid = '{$this-&gt;userid}' AND approved != 1\");\n\n    if(mysql_num_rows($contact_check))\n    {\n        $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-&gt;userid}' AND friend.approved = 1 ORDER BY astatus.onlinestatus DESC\");\n\n        if (mysql_num_rows($friendlook)) {\n            $row = mysql_fetch_assoc($friendlook);\n            $rows[] = $row;\n            print json_encode($rows);\n        } else {\n            $arr = array(\"error\" =&gt; \"Your friends are not online\");\n            print json_encode($arr);\n        }\n\n        return;\n    }\n\n    $arr = array(\"error\" =&gt; \"No Friends?<br \/>Search above for New users or invite some friends.\");\n    print json_encode($arr);\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>http:\/\/php.net\/manual\/en\/function.mysql-fetch-assoc.php<\/p>\n<blockquote>\n<p>Return Values: Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.&#8221;<\/p>\n<\/blockquote>\n<p>Your select statement returns <code>0<\/code> rows, <code>mysql_fetch_assoc<\/code> returns <code>false<\/code>, you insert that false value into <code>$rows<\/code>, so you get an array containing a single false value.<\/p>\n<p>Use <code>if($row === false)<\/code> or <code>if($row)<\/code> to detect if you received a valid row.<\/p>\n<\/li>\n<li>\n<p>Not exactly an answer to your question, but maybe you want to use something like<\/p>\n<pre><code>public function friendsOutputAsJSON($userid)\n{\n    $this-&gt;userid = $userid;\n    $result = mysql_query(\"\n        SELECT\n            friend.to_userid,\n            info.username, info.firstname, info.lastname, info.status,\n            astatus.onlinestatus\n        FROM\n            user_contact as friend\n        LEFT JOIN\n            user_info as info\n        ON\n            friend.to_userid = info.id\n        LEFT JOIN\n            user_online as astatus\n        ON\n            friend.to_userid = astatus.userid\n        WHERE\n            friend.to_userid = info.id\n            AND friend.from_userid = '\". mysql_real_escape_string($userid). \"'\n            AND friend.approved = 1\n        ORDER BY\n            astatus.onlinestatus DESC\n    \");\n    if ( !$result ) {\n        yourErrorHandler();\n    }\n\n    $rows = array();\n    while( $row=mysql_fetch_assoc($result) ) {\n        $rows[] = $row;\n    }\n\n    if( empty($rows) )\n    {\n        $rows = array(\"error\" =&gt; \"No Friends?<br \/>Search above for New users or invite some friends.\");\n    }\n    print json_encode($arr);\n}\n<\/code><\/pre>\n<p>i.e.<\/p>\n<ul>\n<li>not performing an extra query to check if there are any records (which should use Count(*) if you want to keep it)<\/li>\n<li>fetch all records, instead of only the first<\/li>\n<li>checking if some records have been fetched instead using mysql_num_rows<\/li>\n<li>having only one return\/print &#8220;exit point&#8221; for the function<\/li>\n<li>(possibly preventing sql injections &#8211; depends on what $userid can be\/how it is checked before calling the function)<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>i think json_encode() fails .<\/p>\n<p>i advice you to check the error type :<\/p>\n<pre><code>  switch (json_last_error()) {\n        case JSON_ERROR_NONE:\n            echo ' - No errors';\n        break;\n        case JSON_ERROR_DEPTH:\n            echo ' - Maximum stack depth exceeded';\n        break;\n        case JSON_ERROR_STATE_MISMATCH:\n            echo ' - Underflow or the modes mismatch';\n        break;\n        case JSON_ERROR_CTRL_CHAR:\n            echo ' - Unexpected control character found';\n        break;\n        case JSON_ERROR_SYNTAX:\n            echo ' - Syntax error, malformed JSON';\n        break;\n        case JSON_ERROR_UTF8:\n            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';\n        break;\n        default:\n            echo ' - Unknown error';\n        break;\n    }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>To avoid printing the false, use a <code>if (mysql_num_rows($friendlook))<\/code> (in the $friendlook part) to check whether there are results before trying to print anything.<\/p>\n<p>If there are no results, <strong>nothing will be printed<\/strong>. and you won\u2019t have that annoying issue.<\/p>\n<p>Performance note: Don\u2019t use <strong>print<\/strong>, use <strong>echo<\/strong> instead.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:15. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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-&gt;userid = $userid; $contact_check = mysql_query(&#8221; SELECT * FROM user_contact WHERE from_userid = &#8216;{$this-&gt;userid}&#8217; AND [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1233","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1233"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1233\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}