php codeigniter get_where() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24174889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter get_where() function
提问by sola
I am using active record on an old codeigniter installation and I am running into problems executing multiple queries using the get_where function. for instance the following code
我在旧的 codeigniter 安装上使用活动记录,并且在使用 get_where 函数执行多个查询时遇到问题。例如下面的代码
$this->db->get_Where('activation', array('email'=>'[email protected]'));
echo $this->db->last_query();
$this->db->get_Where('users', array('email'=>'[email protected]'));
echo $this->db->last_query();
the first query generates
第一个查询生成
SELECT * FROM (`activation`) WHERE `email` = '[email protected]'
the second one throws me for a loop and generates
第二个让我循环并生成
SELECT * FROM (`activation`, `users`)
WHERE `email` = '[email protected]' AND `email` = '[email protected]'
Am i supposed to be clearing something?
我应该清理一些东西吗?
回答by Aditya Lepcha
$this->db->get_Where('activation', array('email'=>'[email protected]'))->row();
回答by Sudhir Khadka
Use this:
用这个:
$this->db->select('*')->where('email','[email protected]')->get('activation')->result()
回答by Just_Do_It
You need to use it like this:
你需要像这样使用它:
$this->db->get_where('users', array('email'=>'[email protected]'))->result();
Well apart from using get_where you can simply use it like this:
除了使用 get_where 之外,您还可以像这样简单地使用它:
$this->db->select('*');
$this->db->from('activation');
$this->db->where('email','[email protected]');
or
$this->db->where(array('email'=>'[email protected]'));
回答by Dan
You need to retrieve the results from your query with $this->db->query->result();
您需要从查询中检索结果 $this->db->query->result();
$this->db->get_Where('activation', array('email'=>'[email protected]'));
echo $this->db->last_query();
$result_1 = $this->db->query->result();
$this->db->get_Where('users', array('email'=>'[email protected]'));
echo $this->db->last_query();
$result_2 = $this->db->query->result();
or if you want to return an array use result_array();
instead of result();
或者如果你想返回一个数组使用result_array();
而不是result();
回答by Mohan
$this->db->get_Where('activation', array('email'=>'[email protected]'));
echo $this->db->last_query();
$result_1 = $this->db->get()->result_array();
this will execute the first query and then
这将执行第一个查询,然后
$this->db->get_Where('users', array('email'=>'[email protected]'));
echo $this->db->last_query();
$result_2 = $this->db->get()->result();
this will execute the second query then u can do below code to print the two results
这将执行第二个查询,然后您可以执行以下代码来打印两个结果
echo "<pre>";print_r($result_1);print_r($result_2);
echo "</pre>";
回答by Nithyanandhan M
If you use multiple get_where statements in same function then it will be concatenated. So Better use a condition for those two conditions to execute a single query at a time. For example,
如果在同一个函数中使用多个 get_where 语句,那么它将被连接起来。所以最好对这两个条件使用一个条件来一次执行一个查询。例如,
if(//condition 1)
{
$query=$this->db->get_Where('activation', array('email'=>'[email protected]'));
}
else{
$query=$this->db->get_Where('users', array('email'=>'[email protected]'));
}
//finally get the active record.
echo $this->db->last_query();
Otherwise you wont achieve. Please mention the purpose to get two table records.
否则你不会达到。 请提及获取两个表记录的目的。
回答by Rahul Chandran
$this->db->get_where($table, $where, $limit, $offset)
$where = array('FIELD_NAME'=>value)
result_array();