SQLite PDO bindings not working?

假如想象 提交于 2019-12-24 05:18:16

问题


I feel like I'm losing my mind on this one. I have three simple tables. A user table, a roles table, and a role_user table that joins the user and roles in a many to many relationship.

I have the following code to the roles for a user:

$query = $pdo->prepare('select roles.* from roles inner join role_user on roles.id = role_user.role_id where role_user.user_id = ?');
$query->execute(array('1'));
die(var_dump($query->fetchAll()));

This returns an empty array. No results. However, if I change the code to this, I will get the user's roles:

$query = $pdo->prepare('select roles.* from roles inner join role_user on roles.id = role_user.role_id where role_user.user_id = 1');
$query->execute();
die(var_dump($query->fetchAll()));

Am I missing something totally obvious? Is there something about my SQL that is messing up the bindings? Why doesn't the example with bindings work?


回答1:


This is a bug in PDO: http://bugs.php.net/bug.php?id=45259

As a workaround, the following code, though heavier, should work in PHP5.3:

$query = $pdo->prepare(
    'select roles.* from roles inner join role_user on roles.id = role_user.role_id '
    . 'where role_user.user_id = :id'
);
$query->bindValue(':id', 1, PDO::PARAM_INT);
$query->execute();
die(var_dump($query->fetchAll()));

The latest versions of SQLite have native prepared statements, but I don't think PDO can use them yet (IIRC, PDO's code has no real maintainer, so it does not evolve much). It probably won't change anything, but you could still try to disable the emulation with $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);



来源:https://stackoverflow.com/questions/6039986/sqlite-pdo-bindings-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!