所以我想要获取一个标题= "$someTitle“的帖子的id
$dbh= new PDO("mysql:host=localhost;dbname=NAME",'USER','PASS',
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
$someTitle = '\"If you\'re absent during my struggle, don\'t expect to be present during my success.\" - Will Smith';
$statement = $dbh->prepare("select id from posts where title = :title");
$statement->execute(array(':title' => $someTitle));
$row = $statement->fetch();
echo $row[id];这个例子不起作用,但是如果我把$someTitle改成$someTitle = 'new test';
一切都很好
有什么建议吗?
发布于 2012-02-13 13:02:59
不要自己逃脱。这就是为什么会有预准备语句,它会为您解决SQL注入问题。通过在文本中使用您自己的转义,您告诉DB您想要在包含这些转义的DB中搜索文本--这些转义很可能不在那里。
相反,要有:
$someTitle = '"If you\'re absent during my struggle, don\'t expect to be present during my success." - Will Smith';
^---no escape, and ditto for at the end of the line.请注意,我只对"字符进行了取消转义。由于对字符串本身使用了单引号,因此必须对任何'进行转义,才能使其成为有效的PHP字符串赋值。但是,数据库不会看到\',只能看到原始'。
https://stackoverflow.com/questions/9255803
复制相似问题