当我没有问过它时,PHP返回[false]

时间:2011-10-07 14:26:54

标签: php json printing

我不知道为什么,但是当我运行以下代码并且数据库中没有任何内容时,当我查看PHP页面而不是正确的错误消息时,我收到错误[false]

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);
        }
    }

我想知道是否有人会知道为什么?

5 个答案:

答案 0 :(得分:2)

http://php.net/manual/en/function.mysql-fetch-assoc.php

  

返回值:   返回与获取的行对应的字符串关联数组,如果没有其他行,则返回FALSE。“

您的select语句返回0行,mysql_fetch_assoc返回false,您将该false值插入$rows,因此您获得的数组包含一个false值。< / p>

使用if($row === false)if($row)来检测您是否收到了有效的行。

答案 1 :(得分:1)

您有2个SQL查询,并且只检查第一个是否有任何结果。问题是如果第一个有结果而第二个没有结果,你就不会收到错误信息,而是你现在收到的[false]

您的代码应该类似于:

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?<br/>Search above for New users or invite some friends.");
    print json_encode($arr);
}

答案 2 :(得分:0)

为避免打印false,请在尝试打印任何内容之前使用if (mysql_num_rows($friendlook))(在$ friendlook部分中)检查是否有结果。

如果没有结果,将不会打印任何内容。你不会有这个恼人的问题。

效果说明:请勿使用打印,请改用 echo

答案 3 :(得分:0)

不完全是您问题的答案,但也许您想要使用类似

的内容
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?<br/>Search above for New users or invite some friends.");
    }
    print json_encode($arr);
}

  • 没有执行额外的查询来检查是否有任何记录(如果你想保留它,应该使用Count(*))
  • 获取所有记录,而不仅仅是第一个
  • 使用mysql_num_rows
  • 检查是否已提取某些记录
  • 只有一个函数返回/打印“退出点”
  • (可能阻止sql注入 - 取决于$ userid可以是什么/如何在调用函数之前检查它)

答案 4 :(得分:0)

我认为json_encode()失败了。

我建议您检查错误类型:

  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;
    }