• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple question i guess... need help using the db class!

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

I've got this function:

Code:
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;
        }
        else
        {
            return false;
        }
    }

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

I did this after seeing this tutorial:
http://net.tutsplus.com/articles/news/co...tch-day-2/

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 :$?

#2
[eluser]Matalina[/eluser]
if you want an array of results why don't you use:

Code:
$resultarray = $q->result_array();

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

#4
[eluser]CroNiX[/eluser]
Also your original problem was because you used:
Code:
foreach ($q->result as $row) {
instead of
Code:
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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.