CodeIgniter带有Where子句的Select语句


CodeIgniter Select Statement with Where clause

嗨,我是新来的CodeIgniter,我只是想知道我将如何从我的MySql数据库查询,一个有where子句的选择语句,我知道它可以从网上搜索,但每当我尝试一些我得到错误,这真的很令人沮丧。Where子句中的字符串将来自用户输入。谢谢你们了!

您可以按照Mehedi-PSTU所述进行操作,但是似乎您对此有点陌生,因此这里有一些额外的信息:

我将复制Mehedi-PSTU的大部分内容。

$this->get->where('column_name', $equals_this_variable);
$query = $this->db->get('table_name');

这将把查询对象存储在变量$query中。如果您想将其转换为可用的数组,只需执行以下命令。

$results = $query->result_array();

或者你可以这样循环:

foreach($query->result_array() as $result){
    // Perform some task here.
}

更好甚至更全面的理解可能来自:

http://ellislab.com/codeigniter/user-guide/database/active_record.html

试试这样

    $this->db->where('db_attr', $var);
    return $this->db->get('table');

试试这个

$id = 'your id';
$this->db->select("*");
$this->db->from("table_name");
$this->db->where('id','$id');
$query = $this->db->get();
return $query->result_array();

使用方法链接样式的Codeigniter:-

 $data['getData'] = $this->db->get_where('table_name',array('column_name'=>$var))->result_array();