mysql使用JOIN [duplicate]与PDO请求有歧义

8tntrjer  于 5个月前  发布在  Mysql
关注(0)|答案(1)|浏览(48)

此问题在此处已有答案

PDO valid characters for placeholders(2个答案)
23天前关闭
在mysql / phpmyadmin中运行以下语句成功:

SELECT longitude,latitude FROM event JOIN location ON event.locationId=location.id WHERE 
event.id=1234

字符串
在php 8.1中用PDO运行它,它说“id”在where子句中是模糊的。

SELECT longitude,latitude FROM event JOIN location ON event.locationId=location.id WHERE 
event.id=:event-id

$stmt->execute([':event-id' => 1234]);

Select failed: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous


为什么它不能处理已经完全限定的表名?
提前感谢罗伯特

8aqjt8rx

8aqjt8rx1#

不能将-用作PDO参数占位符的一部分。
PDO认为占位符名称是:event,其余部分是SQL的一部分-因此,如果您的事件ID是1234,则最终查询将结束为

SELECT longitude,latitude FROM event JOIN location ON event.locationId=location.id WHERE 
event.id=1234-id

字符串
也就是说,它会尝试从值1234中减去id的值-当然,在这一点上,id是模糊的,因为不清楚你指的是哪个表。
你需要写一些像。

SELECT longitude,latitude FROM event JOIN location ON event.locationId=location.id WHERE 
event.id=:eventID


$stmt->execute([':eventID' => 1234]);


问题演示:https://phpize.online/s/jp
修复程序:https://phpize.online/s/wp

相关问题