只是想知道如何使用Active Record在CodeIgniter中最好地实现以下功能:
$language = 'nl'; // or 'en', 'sp', etc.
$this->db->select('id, description_'.$language.' as description, some_more_fields');
$q = $this->db->get('table_name');
如果我这样使用它$q
将不是一个正确的数据库结果对象,我将在以后使用它时会出现致命错误:
PHP Fatal error: Call to a member function num_rows() on a non-object in /somescript.php on line 12345
此外,$language
变量来自用户输入,因此应该正确转义..
谢谢!
答案 0 :(得分:1)
你引用错了,
$this->db->select('id, description_'.$language.' as description, some_more_fields');
答案 1 :(得分:1)
$ q将是一个对象数组,因此使用如下:
foreach ($q->result() as $row)
{
echo $row->id;
echo $row->description;
echo $row->some_more_fields;
}
(http://ellislab.com/codeigniter/user-guide/database/results.html)