SQLite PDO 绑定不起作用?

发布于 2024-11-08 13:35:44 字数 654 浏览 0 评论 0原文

我觉得我在这件事上失去了理智。我有三张简单的桌子。用户表、角色表和以多对多关系连接用户和角色的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2024-11-15 13:35:44

这是 PDO 中的错误: http://bugs.php.net/bug.php? id=45259

作为一种解决方法,以下代码虽然较重,但应该可以在 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()));

最新版本的 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:

$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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文