簡體   English   中英

Codeigniter db選擇哪里作為數組

[英]Codeigniter db select where as an array

我有此代碼,我需要使用它來從數據庫中獲取結果,我正在使用codeigniter。 這就是查詢的樣子,

        $this->db->select('*');
        $this->db->where('broadcast_id', $bid);
        $query = $this->db->get('ih_noticeboard');
        $result = $query->result();
        if($query->num_rows() < 1){
            $data = "no feed ";
        }else{
            $data = $result;
        }

where子句中的$bid從此代碼獲取,

    $this->db->select('broadcast_id');
    $this->db->where('broadcast_subscribers', $id);
    $query = $this->db->get('ih_broadcastors    ');
    if($query->num_rows() < 1){
        $data = "user not subscribed to any broadcasting";
    }else{
        $ress = $query->result();
        foreach ($ress as $row){
            $bid = $row->broadcast_id;
        }`

現在選擇僅使用一個$ bid,如何將$ bid用作數組?

這很簡單。 只需使用$ this-> db-> where_in('broadcast_id',$ bid);

相反,如果$ this-> db-> where('broadcast_id',$ bid);

https://ellislab.com/codeigniter/user-guide/database/active_record.html#select

有一個查詢來獲取提要更好嗎?

$this->db->select("ih_noticeboard.*");
$this->db->from("ih_noticeboard");
$this->db->join("ih_broadcastors", "ih_noticeboard.broadcast_id = ih_broadcasters.broadcast_id");
$this->db->where("ih_broadcastors.broadcast_subscribers", $id);
$result->db->get()->result();

假設您要在表格中選擇

例如,name ='jane'和broadcast_id = 2

        $bid=array(
           'name'=>'jane',
           'broadcast_id'=>'2'
                  ) 

        $this->db->where($bid);
        $this->db->from($table_name);
        $query = $this->db->get()->result_array();
        return $query;

請注意,必須將$ bid as數組格式化為適合要查詢的下一個數據庫表的格式。 您可以使用foreach循環執行此操作

   foreach ($ress as $row){
        $bid = array(
             'name'=>$row['name'],
             'broadcast_id'=>$row['id']
         );
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM