我有以下可以在Workbench中成功测试的SQL查询:
$interviewInfo = $this->db->fetchAll("
SELECT
c.s,
c.t,
i.u,
i.v,
qa.w,
i.x
FROM
XXX AS i,
YYY as ip,
ZZZ AS qa,
BBB AS c
WHERE
c.s = :CompanyId AND ip.r = i.Id
AND i.s = c.s
AND ip.t = qa.p
AND i.h > 0
ORDER BY i.q DESC
LIMIT 3", array("CompanyId"=>$companyId));
但是在我的SQL语句中使用关联数组依赖项注入后,它返回了一个空数组。
我尝试直接插入CompanyId代替:CompanyId
,然后它可以工作。
我不知道这里发生了什么,因为:
CompanyId
是否存在并且是有效整数。答案 0 :(得分:4)
这不是PDOStatement::fetchAll
的工作方式。您必须先prepare进行查询,然后execute进行查询,然后才能调用fetchAll
来获取结果。这样的事情应该起作用:
$stmt = $this->db-prepare("SELECT
c.s,
c.t,
i.u,
i.v,
qa.w,
i.x
FROM
XXX AS i,
YYY as ip,
ZZZ AS qa,
BBB AS c
WHERE
c.s = :CompanyId AND ip.r = i.Id
AND i.s = c.s
AND ip.t = qa.p
AND i.h > 0
ORDER BY i.q DESC
LIMIT 3");
$stmt->execute(array("CompanyId"=>$companyId));
$interviewInfo = $stmt->fetchAll();
请注意,您还应通过检查prepare
不是execute
且$stmt
不返回false来检查false
和execute
语句是否成功