Welcome Guest, Not a member yet? Register   Sign In
Simple question i guess... need help using the db class!

[eluser]Jason van der Zeeuw[/eluser]
hey all...

I've got this function:

function get_employees($tablename)
        $result = $this->db->query("Select firstname from {$tablename}");
        if($result->num_rows() > 0)
            foreach ($q->result as $row) {
                $resultarray[] = $row;
            return $resultarray;
            return false;

I have the database class loaded automatically in:
$autoload['libraries'] = array('database');

I did this after seeing this tutorial:

but still I get some errors:

Message: Undefined property: CI_DB_mysql_result::$result
Message: Invalid argument supplied for foreach()
Message: Undefined variable: resultarray

While I do exactly the same as in the tutorial...

please can anyone help me :$?

if you want an array of results why don't you use:

$resultarray = $q->result_array();

[eluser]Jason van der Zeeuw[/eluser]
Wow nice one!

thank you for helping me out with this one!!!
it works Big Grin
I didn't knew that function...

Also your original problem was because you used:
foreach ($q->result as $row) {
instead of
foreach ($q->result() as $row) {  //result() is a method, not a property.
That's why you got "undefined property" and it never entered the loop, which made $resultarray undefined as it never got created (but you return it anyway). You should create the $resultarray array (empty) just before the foreach loop where you populate it to prevent that.

Theme © iAndrew 2016 - Forum software by © MyBB