dongxu6418 2014-04-21 12:11
浏览 31
已采纳

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

I'm trying to query users Level but I'm not able to return an int. I have no idea why because when I echo the result it gives is i.e "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;

}

I also tried this but I get 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条回答 默认 最新

  • douqi3195 2014-04-21 12:17
    关注

    as per mysqli <kbd>doc</kbd> you need to query after that you need to fetch the result.

    try this:

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

    it should return user level.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测