【问题标题】:Dealing with Parentheses in Codeigniter Queries处理 Codeigniter 查询中的括号
【发布时间】:2013-02-07 22:14:43
【问题描述】:

我有以下代码块,预计会返回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'

当我直接在我的PostgresqlIDE 上执行此查询时,我得到了count 的输出为2

这个查询可能在哪里出错?我怀疑()WHERE 子句中的存在。我该如何解决这个问题?

更新

当我启用探查器时,我得到了以下查询,

SELECT sum(count) as count 
            FROM multipleowners WHERE owner = 'Davenports Harbour Trustee (2012) Limited'  and locality = 'Auckland Central'

所以很明显问题存在于() 上!!

【问题讨论】:

  • 在这种情况下,分析器不是处于最佳状态。尝试从 sql 命令提示符运行查询,看看括号是否仍然令人悲伤。我怀疑他们会这样做。
  • 不,我尝试在 IDE 上运行查询,我得到了预期的输出。
  • 我尝试在不使用查询绑定的情况下运行查询,并且得到了正确的输出。这证实了问题在于查询被编码。
  • 值得尝试 mysqli 和 pdo 驱动程序,看看它们是否更好。 - 但现在我看到了 postgresql 标签。没关系。

标签: php codeigniter postgresql activerecord


【解决方案1】:

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

$owner = html_entity_decode($owner);

【讨论】:

    【解决方案2】:

    您是否尝试过 select sum(db.[count] as icount,因为在语言中 count 也可能是保留字。所以将该字段括起来或重命名,尽量不要将新输出命名为 count..

    【讨论】:

    • 这绝对不是这里的问题。不过感谢您的建议!
    猜你喜欢
    • 2014-06-26
    • 2011-09-27
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多