[英]PHP: PDO Query returns no results, but the same query returns 4 results in phpmyadmin?
I have written a query to return all comments for a post, excluding blocked users from that post. 我写了一个查询以返回该帖子的所有评论,但不包括该帖子中被阻止的用户。 I have tested the query in phpmyadmin and I get 4/5 possible comments back for the given post (where 1 user is blocked).
我已经在phpmyadmin中测试了查询,对于给定的帖子(其中1个用户被阻止),我得到了4/5条可能的评论。
The query looks like: 查询看起来像:
$query = "SELECT ent.Entity_Id, ent.Profile_Pic_Url, ent.First_Name, ent.Last_Name, ent.Last_CheckIn_Place, comments.Content
FROM checkin_comments AS comments
JOIN entity AS ent
ON comments.Entity_Id = ent.Entity_Id
LEFT JOIN friends AS f
ON ent.Entity_Id = :entityId
WHERE comments.Chk_Id = :checkInId
AND f.Category != 4
GROUP BY comments.Comment_Id
";
// Bind the parameters to the query
$data = Array(":checkInId" => (int)$checkInId, ":entityId" => (int)$userId);
If I run the query on phpmyadmin with the values 1726
for checkinId and 1517
for userId I get the expected outcome, however in PHP I get 0 results. 如果我在phpmyadmin上运行查询,其中checkinId值为
1726
,userId值为1517
,则得到预期的结果,但是在PHP中,我得到0个结果。 I used var_dump to print the contents of data and it shows as: 我使用var_dump来打印数据的内容,它显示为:
array(2) {
[":checkInId"]=>
int(1726)
[":entityId"]=>
int(1517)
}
Why am I experiencing different results in PHP? 为什么我在PHP中遇到不同的结果? All my other queries run fine
我所有其他查询运行正常
EDIT If I swap the bind variables for number values the query works fine, which leads me to believe this is a problem with PDO binding the values to the query. 编辑如果我将绑定变量交换为数字值,则查询工作正常,这使我认为这是PDO将值绑定到查询的问题。 When I perform the bind I use my PDO wrapper class which executes the following methods:
当执行绑定时,我使用我的PDO包装器类,该类执行以下方法:
public function fetchAll($query, $data = null)
{
$stmt = $this->prepareQuery($query, $data);
return $stmt->fetchAll();
}
private function prepareQuery($query, $data = null)
{
$stmt = $this->connection->prepare($query);
$stmt->execute($data);
return $stmt;
}
Does this scream the answer to any more experienced users of PDO? 这是否会给更多经验丰富的PDO用户带来答案?
I think you were not including the params in single quotes. 我认为您没有在单引号中包含参数。 Try this code
试试这个代码
$query = "SELECT `ent`.`Entity_Id`, `ent`.`Profile_Pic_Url`, `ent`.`First_Name`, `ent`.`Last_Name`, `ent`.`Last_CheckIn_Place`, `comments`.`Content`
FROM `checkin_comments` AS `comments`
JOIN `entity` AS `ent`
ON `comments`.`Entity_Id` = `ent`.`Entity_Id`
LEFT JOIN `friends` AS `f`
ON `ent`.`Entity_Id` = ':entityId'
WHERE `comments`.`Chk_Id` = ':checkInId'
AND `f`.`Category` != 4
GROUP BY `comments`.`Comment_Id`
";
// Bind the parameters to the query
$data = Array(":checkInId" => (int)$checkInId, ":entityId" => (int)$userId);
I hope this will help. 我希望这将有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.