PHP PDO Prepared Statement Query不替代

时间:2011-12-05 21:23:23

标签: php mysql pdo prepared-statement

我有一小段PHP代码:

$stm = $db->prepare($sql);
$result = $stm->execute($params);

$ params是以下

array(1) {
  [0]=>
  string(3) "why"
}

但是,发送到MySQL的$ sql仍然包含一个问号,根据MySQL日志:

1 Query SELECT a.* FROM article a
LEFT JOIN article_links al on a.id = al.from_article_id
WHERE al.to_article = '?'

我还缺少其他替代品吗?

2 个答案:

答案 0 :(得分:6)

您不需要在问号周围加上单引号:

SELECT a.* FROM article a
LEFT JOIN article_links al on a.id = al.from_article_id
WHERE al.to_article = ?;

答案 1 :(得分:2)

我怀疑这是真的:

$sql = "SELECT a.* FROM article a LEFT JOIN article_links al on a.id = al.from_article_id WHERE al.to_article = '?'"

问号不应该用引号括起来 - 这意味着它是一个字符串而不是参数占位符。仅使用al.to_article = ?