Welcome Guest, Not a member yet? Register   Sign In
Database Problem with returning information.
#1

[eluser]Jordz[/eluser]
G'day All,

I seem to be having trouble with returning information from my database.
In my controller I have:
Code:
function some(){
        echo "<pre>";
        $queue = $this->db->get_where('airports', array('icao'=>'ENGM'));
        $test = $queue->result();
        
        print_r( $test);
        
    }

This returns the information from the database:

Array
(
[0] => stdClass Object
(
[id] => 170
[icao] => ENGM
[iata] => OSL
[country] => Norway
[name] => Oslo Gardermoen
)

)

However, when I attempt to access the data in the array like this $test->name I get an error:
Quote:Severity: Notice


Message: Trying to get property of non-object


Filename: controllers/screening.php


Line Number: 49

I've tried everything and cannot access the array at all.
Any idea's?

Thanks

Jordan
#2

[eluser]danmontgomery[/eluser]
Code:
$queue = $this->db->get_where('airports', array('icao'=>'ENGM'));
if($queue) {
    foreach($queue->result() as $row) {
        echo $row->country;
        echo $row->name;
        // etc
    }
}

or, if only one row is returned

Code:
$queue = $this->db->get_where('airports', array('icao'=>'ENGM'));
if($queue) {
    $row = $queue->row();
    echo $row->country;
    echo $row->name;
    // etc
}
#3

[eluser]WanWizard[/eluser]
As your print_r() output shows, an array of objects is returned, and not a single object.
You should therefore access this one as $test[0]->name;
#4

[eluser]Jordz[/eluser]
Ahhh, thanks for those comments it worked Smile

You see, in my controller I have:
Code:
$data['photos'] = $queue->result();
    //    print_r($photos);
    
    $this->load->view('queue', $data);

and In my actual view file I can access the array by going $photo->icao if I wanted to, but you can't in the controller so It threw me a little.

Thanks for the help,

Jordan
#5

[eluser]Unknown[/eluser]
[quote author="WanWizard" date="1270597249"]As your print_r() output shows, an array of objects is returned, and not a single object.
You should therefore access this one as $test[0]->name;[/quote]

I've been looking for this solution for weeks..

Thanks!!




Theme © iAndrew 2016 - Forum software by © MyBB