【问题标题】:CodeIgniter - Binding a EXIST subqueryCodeIgniter - 绑定 EXIST 子查询
【发布时间】:2017-05-28 01:44:54
【问题描述】:

首先,我是该框架的新手。检查我尝试使用 CI 数据库对象转录的查询。

$where = "(".$this->tbl.".invoiceNumber = '".substr($searchFor,3)."' 
          OR EXISTS (SELECT 1 FROM op_orders_products WHERE idProduct = ".(is_numeric(substr($searchFor,3)) ? substr($searchFor,3) : '-1')." 
          AND idOrder = ".$this->tbl.".id)
       )";

我应该做一个单独的子查询吗?想把这一切合二为一。

我就是这样开始的。我想确保变量是绑定的,而不是像原始查询中那样作为字符串传递。

$this->db->group_start()
           ->where($this->tbl.".invoiceNumber", substr($searchFor, 3))
           ->or_group_start()
             // I'm missing this EXISTS select subquery
           ->group_end()
         ->group_end()

非常感谢您的帮助。

【问题讨论】:

    标签: php mysql sql codeigniter


    【解决方案1】:

    据我所知,Codeigniter 的查询生成器类中没有 EXISTS 等价物。所以我会这样写:

    $this->db->group_start()
                 ->where($where)
             ->group_end()
    

    参见文档here

    binding:

    使用 CodeIgniter,您可以像这样将变量绑定到您的查询:

    $sql = "SELECT * FROM table WHERE a= ? AND b= ?";
    $query = $this->db->query($sql, array($a,$b));
    

    这会将变量 $a 和 $b 绑定到查询字符串中的相应位置 (?)。

    escaping

    $this->db->escape()
    

    此函数转义字符串数据并在其周围添加单引号。

    【讨论】:

    • 嗨 Vickel,出于安全原因,我想在 $where 子句中绑定我的变量。
    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 2011-06-11
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    相关资源
    最近更新 更多