简体   繁体   English

准备好的PDO查询返回0个受影响的行

[英]Prepared PDO query returns 0 affected rows

I've literally spent hours Googling and searching and I have no idea what I'm doing wrong. 我已经花了数小时在Google搜索和搜索上,却不知道自己在做什么错。 I'm using POST, and I tried the query myself in phpMyAdmin, and it worked, but it's not working in PDO, and I don't know why. 我使用的是POST,我自己在phpMyAdmin中尝试了该查询,并且该查询有效,但在PDO中却无法正常工作,我也不知道为什么。 It worked when I used mysql_query and what not, but I want to protect myself from SQL injection. 当我使用mysql_query时,它起作用了,但不行,但是我想保护自己免受SQL注入的侵害。

Here's the code I'm using: 这是我正在使用的代码:

<?php
    if(isset($_POST['user']) and isset($_POST['pass'])) {
        $name = $_POST['user'];
        $pass = $_POST['pass'];

        $db = new PDO('mysql:host=localhost;dbname=*****;charset=utf8', '*****', '*****');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $query = $db->prepare("SELECT * FROM stored WHERE _User LIKE :username AND _Pass LIKE :password");
        $query->bindValue(":username", $user, PDO::PARAM_STR);
        $query->bindValue(":password", $pass, PDO::PARAM_STR);

        $query->execute();

        echo $query->rowCount();
    }
?>

看起来您在第41行中命名了用户名变量$ name,但是在第46行绑定该参数时,将其命名为$ user。

Either you are using ancient version of PHP, or there are no rows in your database matching the criteria. 您正在使用旧版本的PHP,或者数据库中没有符合条件的行。 Upgrade for the first and check your data and query for the second. 升级第一个,然后检查数据并查询第二个。

Also, as Holy Linus spake to us, "Talk Is Cheap, Show Me The Code". 同样,当圣莱纳斯对我们说话时,“说话便宜,给我看密码”。
Don't tell us "It worked when I used blah blah query". 不要告诉我们“当我使用等等查询时它起作用了”。 Run it. 运行。 In the very same file, next to very this code. 在相同的文件中,非常靠近此代码。 This way you will have a proof. 这样,您将有一个证明。 Only such a proof matters, while your speculations aren't. 只有这样的证明很重要,而您的猜测却没有。

However, strictly speaking, you don't need this function at all, as well as most of your code 但是,严格来说,您和大多数代码都根本不需要此功能

$stmt = $db->prepare("SELECT 1 FROM stored WHERE _User = ? AND _Pass = ?");
$stmt->execute(array($_POST['user'],$_POST['pass']));
echo $stmt->fetchColumn();

is enough. 足够。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM