php 在查询中使用 '' 而不将值转换为指针

php use '' in a query without convert the value to a pointer

我需要执行一个 sql 查询,我需要在其中使用撇号,因为我要搜索电子邮件地址。当我现在在查询中键入 '' 时,它们变成了一个变量指针,但它们应该只告诉 mysql 这是一个字符串。

如何告诉程序以不同的方式使用它们?我已经试过了 pdo->quote.

$statement = $pdo->prepare("SELECT user_id FROM tbl_server WHERE user_id = (SELECT user_id from tbl_user where Email = ':email') "); // here i need the normal apostrophe
$result = $statement->execute(array('email' => $email));
$user = $statement->fetch();

您不得在查询中引用 :email;数据库驱动程序将确定如何处理参数,因为它知道传递的类型和数据库中列的类型。但是你需要在执行参数化查询时将:添加到email之前。

$statement = $pdo->prepare("SELECT user_id FROM tbl_server WHERE user_id = (SELECT user_id from tbl_user where Email = :email) "); // here i need the normal apostrophe
$result = $statement->execute(array(':email' => $email));
$user = $statement->fetch();

顺便说一下,您应该检查 $resulttrue 还是 false,而且您的查询可能会提供一个空结果集。所以你应该在获取后检查是否$user === false(即没有找到用户)。