CodeIgniter Forums
Problem with active record - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Problem with active record (/showthread.php?tid=7423)



Problem with active record - El Forum - 04-08-2008

[eluser]rhasan[/eluser]
Hi, I am running the following code from a controller
Code:
$this -> db -> select('age', 'name');
$query = $this -> db -> get('info');
foreach ( $query -> result() as $row){
    echo $row -> name;
    echo $row -> age;
    echo br();
}
and expecting that it will show both age and name of users from info table but i am getting the following error
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$name
Filename: controllers/blog.php
Line Number: 37
It is showing only the age but not showing the name. How to correct this error?


Problem with active record - El Forum - 04-08-2008

[eluser]barbazul[/eluser]
the select() method takes only 1 parameter
simply change to look like this:

Code:
$this -> db -> select('age,name');
$query = $this -> db -> get('info');
foreach ( $query -> result() as $row){
    echo $row -> name;
    echo $row -> age;
    echo br();
}

It could be interesting to request passing multiple parameters as a supported feature though


Problem with active record - El Forum - 04-08-2008

[eluser]Seppo[/eluser]
As a matter of fact, select accept two parameters. The second is used to avoid field escaping.
However in this case, an array of fields can be used.
Code:
$this->db->select(array('age', 'name'));

BTW, @Barbazul Regards from Cris & Pedro =)


Problem with active record - El Forum - 04-08-2008

[eluser]rhasan[/eluser]
Thanks both of you for such quick reply