PHP中的SQL查询未返回预期结果

时间:2014-04-21 12:11:09

标签: php mysql

我尝试查询用户级别,但我无法返回int。我不知道为什么,因为当我回应它给出的结果时,即" 1"

function lvl_chcek($username)
{
    $db = new mysqli('localhost', 'root', 'root', 'idoctor_db');

    $lvl = $db->query('
    SELECT Level
    FROM users
    WHERE Login = "'.$username.'"
    ');

    echo $lvl->fetch_object()->Level;

    return $lvl->fetch_object()->Level;

}

我也试过了,但我得到了NULL

function lvl_chcek($username)
{
    $db = new mysqli('localhost', 'root', 'root', 'idoctor_db');

    $query = $db->query('
    SELECT Level
    FROM users
    WHERE Login = "'.$username.'"
    ');
    $result = mysql_query($query);
    $array = mysql_fetch_assoc($result);
    $lvl = $array['Level'];

    echo $lvl;

    return $lvl;
}

1 个答案:

答案 0 :(得分:0)

根据mysqli doc ,您需要在获取结果后进行查询。

试试这个:

function lvl_chcek($username)
{
    $db = new mysqli('localhost', 'root', 'root', 'idoctor_db');

   $query = $db->query('
    SELECT Level
     FROM users
     WHERE Login = "'.$username.'"
     LIMIT 1');

    $obj = $query->fetch_object();
    return $obj->Level;

}

它应该返回用户级别。