Welcome Guest, Not a member yet? Register   Sign In
Using a table name as a variable in Active Record Query
#1

[eluser]Unknown[/eluser]
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:
Code:
$query = $this->db->get('my_table');

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

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

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

Code:
$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.
#2

[eluser]PhilTem[/eluser]
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

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

in your code?
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB