简体   繁体   English

Codeigniter db选择哪里作为数组

[英]Codeigniter db select where as an array

I have this code i need to use to get results from a database, I'm on codeigniter. 我有此代码,我需要使用它来从数据库中获取结果,我正在使用codeigniter。 This is how the query looks like, 这就是查询的样子,

        $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;
        }

the $bid in the where clause is got from this code, 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;
        }`

Now the select uses only one $bid, how can I use the $bid as an array? 现在选择仅使用一个$ bid,如何将$ bid用作数组?

It's quite simple . 这很简单。 just use $this->db->where_in('broadcast_id', $bid); 只需使用$ this-> db-> where_in('broadcast_id',$ bid);

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

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

Wouldn't it be better to have one query to get the feeds? 有一个查询来获取提要更好吗?

$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();

Assuming you want to select in the table where 假设您要在表格中选择

name='jane' and broadcast_id=2 #as an example 例如,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;

Please notice that the $bid as array must be formated to suite the next database table to be queried. 请注意,必须将$ bid as数组格式化为适合要查询的下一个数据库表的格式。 You can do this using foreach loop Eg 您可以使用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