![]() |
Using a table name as a variable in Active Record Query - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Using a table name as a variable in Active Record Query (/showthread.php?tid=53210) |
Using a table name as a variable in Active Record Query - El Forum - 07-15-2012 [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. Using a table name as a variable in Active Record Query - El Forum - 07-15-2012 [eluser]PhilTem[/eluser] It must work. I got the same code running successfully on many pages ![]() 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) in your code? Using a table name as a variable in Active Record Query - El Forum - 07-15-2012 [eluser]Unknown[/eluser] You are right! I copied and pasted code...but I didn't copy the most important part! Thanks! =) |