Php 在Codeigniter查询中处理括号
我有下面的代码块,它将返回Php 在Codeigniter查询中处理括号,php,codeigniter,postgresql,activerecord,Php,Codeigniter,Postgresql,Activerecord,我有下面的代码块,它将返回计数 $sql = "SELECT sum(count) as count FROM multipleowners WHERE owner = ? " . $localityquery; $queryarray = array($owner, $locality); $query = $this->db->query($sql, $queryarray); if ($query->num_row
计数
$sql = "SELECT sum(count) as count
FROM multipleowners WHERE owner = ? " . $localityquery;
$queryarray = array($owner, $locality);
$query = $this->db->query($sql, $queryarray);
if ($query->num_rows() > 0)
{
$result = $query->row_array();
$count = $result['count'];
}
但当我尝试打印$count
时,得到的是空值
我使用了print\r($this->db->last\u query())代码>我得到了以下查询
SELECT sum(count) as count FROM multipleowners WHERE owner = 'Davenports Harbour Trustee (2012) Limited' and locality = 'Auckland Central'
SELECT sum(count) as count
FROM multipleowners WHERE owner = 'Davenports Harbour Trustee (2012) Limited' and locality = 'Auckland Central'
当我直接在我的Postgresql
IDE
上执行此查询时,我得到了count
作为2
的输出
这个问题会在什么地方出错?我怀疑WHERE
子句中是否存在(
和)
。我该如何解决这个问题
更新
启用探查器时,我得到以下查询:
SELECT sum(count) as count FROM multipleowners WHERE owner = 'Davenports Harbour Trustee (2012) Limited' and locality = 'Auckland Central'
SELECT sum(count) as count
FROM multipleowners WHERE owner = 'Davenports Harbour Trustee (2012) Limited' and locality = 'Auckland Central'
很明显,问题存在于(
和)
您是否尝试过将select sum(db.[count]作为icount,因为在语言中count也可以是保留字。因此,请将字段括起来或重命名,尽量不要将新输出命名为count。答对了!!在将变量$owner
传递到查询之前,我添加了以下行,并且成功了
$owner = html_entity_decode($owner);
在这种情况下,探查器不是最佳状态。请尝试从sql命令提示符运行查询,并查看括号是否仍然给出错误信息。我怀疑它们是否给出错误信息。不,我尝试在IDE上运行查询,得到了预期的输出。我尝试在不使用查询绑定的情况下运行查询,得到了正确的输出。因此,这确认了issue正在对查询进行编码。值得尝试mysqli和pdo驱动程序,看看它们是否更好。-但现在我看到了postgresql标记。没关系。这绝对不是问题所在。不过,感谢您的建议!!