CodeIgniter选择查询 [英] CodeIgniter Select Query
问题描述
我有一个简单的CodeIgniter活动记录查询来选择一个ID:
I have a simple CodeIgniter Active record query to select an ID:
$q = $this -> db
-> select('id')
-> where('email', $email)
-> limit(1)
-> get('users');
如何将上述ID分配给变量(例如$ id)
示例,
How can I assign the above ID to a variable (eg. $id) For example,
echo "ID is" . $id;
谢谢。
推荐答案
这很简单。例如,这里是我的随机代码:
Thats quite simple. For example, here is a random code of mine:
function news_get_by_id ( $news_id )
{
$this->db->select('*');
$this->db->select("DATE_FORMAT( date, '%d.%m.%Y' ) as date_human", FALSE );
$this->db->select("DATE_FORMAT( date, '%H:%i') as time_human", FALSE );
$this->db->from('news');
$this->db->where('news_id', $news_id );
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
$row = $query->row_array();
return $row;
}
}
您选择作为数组,以便您可以访问它:
This will return the "row" you selected as an array so you can access it like:
$array = news_get_by_id ( 1 );
echo $array['date_human'];
我也强烈建议,不链接 。总是让他们分开像我的代码,这显然是很容易阅读。
I also would strongly advise, not to chain the query like you do. Always have them separately like in my code, which is clearly a lot easier to read.
请注意,如果在from()中指定表名称,则调用不带参数的get()函数。
Please also note that if you specify the table name in from(), you call the get() function without a parameter.
如果您不明白,请随时问:)
If you did not understand, feel free to ask :)
这篇关于CodeIgniter选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!