PHP-MySQLi 准备语句在代码中不起作用,但工作是 PHPMyAdmin


PHP-MySQLi Prepared Statement not working in code but working is PHPMyAdmin

我正在运行一个预准备语句,出现以下错误。我不明白问题是什么。由于相同的查询在 PHPMyAdmin 中工作正常。

法典

$queryConstruct = "
    SELECT id FROM registrations WHERE 
        email = '$email' 
    AND 
        license_type = $license_type";

错误

致命错误:准备查询时出现问题(从注册中选择 ID,其中电子邮件 = 'test1@gmail.com' 和 license_type = 0) 您的 SQL 语法有错误; 检查与您的MySQL服务器版本相对应的手册,了解在mysqli.lib第2行的**''test1@gmail.com'和license_type = 0' **附近使用的正确语法.php在第444行

准备好的语句看起来像这样

SELECT `id` 
FROM `registgrations` 
WHERE `email` = :email
AND `license_type` = :license_type

查看PDO手册文档以了解如何使用它。

http://php.net/manual/en/class.pdostatement.php

正如罗廷汉所说,这不是一个准备好的陈述。来自 PHP 文档。

if ($stmt = $mysqli->prepare("SELECT id FROM Registrations WHERE email=? AND license_type=?")) {
    $stmt->bind_param("s", $email);
    $stmt->bind_param("s", $license_type);
    $stmt->execute();
    $stmt->bind_result($id);
    $stmt->fetch();
    printf("%s - %s - %s'n", $id, $email, $license_tpe)
    $stmt->close();
}

不过,不确定我是否正确编辑。无论如何,这应该暗示你所做的不是准备好的陈述。

请注意,不是

连接查询中的实际值,而是将它们替换为占位符(在本例中为"?"),然后由值绑定。这样,可以防止 SQL 注入操作查询,从而破坏数据。

更重要的是,考虑学习PDO课程以获得更简单的方法。