开发者

PDO Misunderstanding

I am writing my PHP blog engine. I am using PDO for it. And now, when I am writing class Member - I have an error.

Fatal error: Call to a member function fetch() on a non-object in /home/tucnak/Server/scripts/php/classes/Member.php on line 42

And source code of my Class:

public fu开发者_如何学Pythonnction authMember($user, $password)
{
    $password = hashIt($password);
    $count = 100500;
    $count = $this->db->query("SELECT count(*) FROM users-general WHERE nick = $user AND password = $password;")->fetch();
    echo($count);
    // if ($count == 1){ return 1; } else { throw new Exception("",491); }
}

I have an error using this function.


Your query probably fails because you don't have quotes wrapped around your query.

When that happens, query() will return false instead of an object, breaking the chain.

Don't do it this way; run the query first, save its result, then check whether it's false.

By the way, you should really use prepared statements - your current statement is vulnerable to SQL injection.


"SELECT count(*) FROM `users-general` WHERE nick = '$user' AND password = '$password'"

note the single quotes of 2 different types

Thought, your misunderstanding has nothing to do with PDO. it's basic SQL syntax you have to learn

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜