处理Codeigniter查询中的括号

时间:2013-02-07 22:14:43

标签: php codeigniter postgresql activerecord

我有以下代码块,预计会返回count

    $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_query());,我得到了以下查询,

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'

显然问题存在于() !!

2 个答案:

答案 0 :(得分:2)

宾果!!在我将变量$owner传递给查询之前,我添加了以下行,并且它有效,

$owner = html_entity_decode($owner);

答案 1 :(得分:0)

你是否尝试过选择总和(db。[count]作为icount,因为语言计数也可以是保留字。所以括起字段或重命名它,尽量不要将新输出命名为count .. < / p>