Codeigniter 拼接SQL語句

來源:互聯網
上載者:User

CI 中的  $this->db->or_where  跟 $this->db->where  方法只限於第一次執行語句,當執行第二個語句,這些條件就得再寫一遍,

而 get_where 中的 $where變數只接受 array 數組形式,所在想在此寫一個方法拼接SQL 。

好,原本是這樣的

$this->db->where("wddm",$this->session->userdata("wddm")); 

$this->db->or_where("lururen",$this->session->userdata("xm")); 

//第一次執行,where 接在SQL 後面

$this->db->get_where($table, $where); 

//分頁SQL,第二次執行,則需要再將where定一遍 

$this->db->get_where($table, $where); 

/**     * 拼SQL:2012-7-14     */

function queryByWhereSQL($table, $where=null,$ALL=TRUE,$pageSeg=5,$pageSize=12){

             if($ALL){             //顯示所有的資訊,<em>不加LIMIT條件</em> 

                   $sql="select * from $table where $where order by id desc"; 

              }else{

                 $current = $this->uri->segment($pageSeg); 

                 $sql="select * from $table where $where order by id desc LIMIT " . $current*$pageSize.",$pageSize"; 

             } 

            return $this->queryBySql($sql); 

}

   分頁處調用

         if(!is_null($where)&&is_string($where)){
           $this->query= $this->obj->mcom_model->queryByWhereSQL($this->table,$where,FALSE,$this->seg,$this->perpage);
        }else{
          $this->query = $this->obj->mcom_model->queryByWhere($this->table, $where, $this->perpage, $this->perpage * $this->current);  
        }

OVER,哪位有簡單的方法麻煩通告一聲,謝謝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.