简体   繁体   中英

PHP PDO Prepared Statement Query not substituting

I have a short segment of PHP code:

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

$params is the following

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

However, the $sql being sent to MySQL still contains a question mark according to the MySQL logs:

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

Is there something else I'm missing for substitution?

You don't need to have the single quotes around the question mark:

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

I suspect this is true:

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

The question mark should not be surrounded by quotes--this means it's a string instead of a parameter placeholder. Use just al.to_article = ? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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