CodeIgniter 选择查询 [英] CodeIgniter Select Query
本文介绍了CodeIgniter 选择查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的 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() 函数时不带参数.
如果你不明白,请随时问:)
If you did not understand, feel free to ask :)
这篇关于CodeIgniter 选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文