• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using a table name as a variable in Active Record Query

I am an ASP.Net developer and I'm learning PHP/Code Igniter. I like it so far..BUT:

There is probably a very simple answer to this question. I have looked around for the solution, but all examples that I have seen do not use variables as I do here.

This works:
$query = $this->db->get('my_table');

This doesn't:
$query = $this->db->get($tbl);

In my model, I have a method that takes $tbl as an argument:
public function getAll($tbl)

I am passing in the name from my controller as such:

$data['mlist'] = $this->my_model->getAll('my_table');

The error I get is "Message: Invalid argument supplied for foreach()"

But the same foreach loop works when I "hard code" the table name in. I just don't see how this isn't working.

Anyway, thanks in advance for any help.

It must work. I got the same code running successfully on many pages Wink

But it also looks like you run the query but don't return the result (since you get an error with foreach not a MySQL-error). Do you have

function getAll($tbl)
  $query = $this->db->get($tbl);
  return $query->result();

in your code?

You are right! I copied and pasted code...but I didn't copy the most important part! Thanks! =)

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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