无法在准备好的语句中比较来自 mysql 的值

发布于 2024-08-25 22:32:11 字数 603 浏览 4 评论 0原文

我似乎无法让它连接到数据库,以便我可以运行我准备好的语句。有人知道我忘记了什么吗?

private function check_credentials($plain_username, $password)
    {
    global $dbcon;

    $ac = new ac();
    $ac->dbconnect();
    $userid = $dbcon->prepare('SELECT id FROM users WHERE username = :username AND password = :password LIMIT 1');
    $userid->bindParam(':username', $plain_username);
    $userid->bindParam(':password', $password);
    $userid->execute();

    $id = $userid->fetch();
    Return $id;
    }

编辑:我将 SQL 查询从 SELECT FROM 查询更改为 INSERT INTO 查询,并且它有效。到底是怎么回事?

I can't seem to get this to connect to the database so that I can run my prepared statement. Does anybody have an idea what I've forgotten?

private function check_credentials($plain_username, $password)
    {
    global $dbcon;

    $ac = new ac();
    $ac->dbconnect();
    $userid = $dbcon->prepare('SELECT id FROM users WHERE username = :username AND password = :password LIMIT 1');
    $userid->bindParam(':username', $plain_username);
    $userid->bindParam(':password', $password);
    $userid->execute();

    $id = $userid->fetch();
    Return $id;
    }

EDIT: I changed the SQL query from a SELECT FROM query, to an INSERT INTO query and it worked. WHat the heck is going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落墨 2024-09-01 22:32:11

重新格式化堆栈回溯:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound' in E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php:61
Stack trace:
#0 E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php(61): PDOStatement->execute(Array)
#1 E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php(34): ac->check_credentials('joe', '94a02c32b6ff629...')
#2 E:\PortableApps\xampp\htdocs\SN\UI\UIclass.php(17): ac->authentication()
#3 E:\PortableApps\xampp\htdocs\SN\index.php(4): ui->start()
#4 {main} thrown in E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php on line 61

您在 check_credentials 函数中实例化新的 ac 对象有什么原因吗?鉴于 check_credentials 已经是 ac 的方法,这看起来很奇怪。 dbconnect 是否会覆盖全局 dbcon

Reformatting your stack backtrace:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound' in E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php:61
Stack trace:
#0 E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php(61): PDOStatement->execute(Array)
#1 E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php(34): ac->check_credentials('joe', '94a02c32b6ff629...')
#2 E:\PortableApps\xampp\htdocs\SN\UI\UIclass.php(17): ac->authentication()
#3 E:\PortableApps\xampp\htdocs\SN\index.php(4): ui->start()
#4 {main} thrown in E:\PortableApps\xampp\htdocs\SN\AC\ACclass.php on line 61

Is there any reason you're instantiating a new ac object in the check_credentials function? Given that check_credentials is already a method of ac, this seems odd. Does dbconnect overwrite the global dbcon?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文