Welcome Guest, Not a member yet? Register   Sign In
Problem with active record
#1

[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?
#2

[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
#3

[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 =)
#4

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




Theme © iAndrew 2016 - Forum software by © MyBB