duanshan188866 2012-06-14 15:01
浏览 81
已采纳

查询返回null

Hello I've got this query to get users by email, which is an unique field in the db.

However, when i want to get the data on it, it simply returns null.

Here's the code

    public function getUserByEmail($email)
    {

            $statement = "SELECT id_user,nome,email,permissao,activo FROM sys_users 
                          WHERE email=$email";

            try
            {

             $sth = $this->db->query($statement);
             $sth->setFetchMode(PDO::FETCH_OBJ);
             $rcs_users = $sth->fetchAll();
             return $rcs_users;

            }
            catch(PDOException $e)
            {
                "DB Error".$e->getMessage();
            }

    }

And the respective function call

$user_rcs = $user->getUserByEmail($email);

    var_dump($user_rcs); //returns null




    $_SESSION['email'] = $email;
    $_SESSION['user'] = $user_rcs->nome;
    $_SESSION['permissao'] = $user_rcs->permissao;

And then I get this error

Notice: Trying to get property of non-object in C:\xampp\htdocs\inacesso\admin\modules\auth\authhandler.php on line 24

Glad if you could help me!

  • 写回答

2条回答 默认 最新

  • dounei9043 2012-06-14 15:04
    关注

    First off, seriously have a look at PDO.

    Secondly I would imagine the email column is a string. As such, you'll need to surround $email with quotes in your query (after having sanitized it vigorously of course...)

    WHERE email='$email'
    

    PDO version:

    $pdo = new PDO(...);
    $query = $pdo->prepare('SELECT id_user,nome,email,permissao,activo '.
                           'FROM   sys_users '.
                           'WHERE  email = ?');
    $result = $query->execute(array($email));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!