SQLite PDO 绑定不起作用?
我觉得我在这件事上失去了理智。我有三张简单的桌子。用户表、角色表和以多对多关系连接用户和角色的 role_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()));
这将返回一个空数组。没有结果。但是,如果我将代码更改为这样,我将获得用户的角色:
$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()));
我是否遗漏了一些完全明显的东西?我的 SQL 是否有某些内容弄乱了绑定?为什么带有绑定的示例不起作用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 PDO 中的错误: http://bugs.php.net/bug.php? id=45259
作为一种解决方法,以下代码虽然较重,但应该可以在 PHP5.3 中工作:
最新版本的 SQLite 具有本机准备好的语句,但我认为 PDO 还不能使用它们(IIRC、PDO 的代码没有真正的维护者,因此它不会发展太多)。它可能不会改变任何东西,但您仍然可以尝试使用
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0); 禁用模拟
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:
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);