SQLite PDO bindings not working?
I feel like I'm losing my mind on this one. I have three simple tables. A user table开发者_JS百科, 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?
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);
精彩评论