Welcome Guest, Not a member yet? Register   Sign In
How can I execute database queries in a helper function?
#1

[eluser]Unknown[/eluser]
I am writing a helper function in which I need to query the database. I have tried the following in my helper function.

Code:
$this->db->query("SELECT * FROM fish WHERE species='tuna'");

However, I get this error:

Code:
Fatal error: Using $this when not in object context in /home/john/myProjects/application/helpers/fish_helper.php on line 9

What is the proper way to execute a database query in a helper function? Thank you.

--- Update ---

I broke CodeIgniter abstraction barriers and used the mysql_query() function. Is there a way to do this through the CodeIgniter interface?
#2

[eluser]Rolly1971[/eluser]
in your helper function add:

Code:
$ci =& get_instance();

$ci->db->where('species', 'tuna');
$query = $ci->db->get('fish');

return $query->result_array();




Theme © iAndrew 2016 - Forum software by © MyBB