How can I pass an array of PDO parameters yet still specify their types?

前端 未结 3 1766
$sql = \"SELECT * FROM table WHERE id LIKE CONCAT(\'%\', :id, \'%\')
LIMIT :limit1, :limit2\";

I want to still use the array input like this:

3条回答
  •  醉梦人生
    2020-12-01 18:56

    If you turn off the default setting of PDO::ATTR_EMULATE_PREPARES, then it will work. I just found out that that setting is on by default for mysql, which means you never actually use prepared statements, php internally creates dynamic sql for you, quoting the values for you and replacing the placeholders. Ya, a major wtf.

    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $stmt = $pdo->prepare($sql);
    $stmt->execute(array(5)); //works!
    

    The prepares are emulated by default because of performance reasons.

    See as well PDO MySQL: Use PDO::ATTR_EMULATE_PREPARES or not?

提交回复
热议问题