Part of the EllisLab Network
   
 
Cannot query database in custom_helper?
Posted: 30 June 2007 04:50 AM   [ Ignore ]  
Summer Student
Total Posts:  15
Joined  06-15-2007

I have a function placed in custom_helper file :

function getCityName($id)
{
    $query 
$this->db->getwhere('thanhpho', array('ThanhPhoID' => $id));
    
$row $query->row();
    return 
$row->Ten;

i want it automatically called in my view. But the view generated error:

Fatal error: Call to a member function on a non-object in d:\www\hoahau\system\application\helpers\custom_helper.php on line 181

and line 181 is my query command in function above.
What’s the wrong in my code? Please tell.
Thanks for any response

Profile
 
 
Posted: 30 June 2007 10:16 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  165
Joined  08-22-2006
java - 30 June 2007 08:50 AM

I have a function placed in custom_helper file :

function getCityName($id)
{
    $query 
$this->db->getwhere('thanhpho', array('ThanhPhoID' => $id));
    
$row $query->row();
    return 
$row->Ten;

i want it automatically called in my view. But the view generated error:

Fatal error: Call to a member function on a non-object in d:\www\hoahau\system\application\helpers\custom_helper.php on line 181

and line 181 is my query command in function above.
What’s the wrong in my code? Please tell.
Thanks for any response

You have to instantiate CI if you want to use it out the flow, so you should go as follows,

$CI =& get_instance();
    
    function 
getCityName($id){
        $query 
$CI->db->getwhere('thanhpho', array('ThanhPhoID' => $id));
        
$row $query->row();
        return 
$row->Ten;
    
 Signature 

Don’t argue with an idiot, people watching may not be able to tell the difference.


marcoss
http://defmay.com
http://fenix.st

Profile
 
 
Posted: 01 July 2007 09:37 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  15
Joined  06-15-2007

I tried to instantiate CI but it still generate an error. I dont know it’s not understand my query command.

Profile
 
 
Posted: 01 July 2007 11:09 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  65
Joined  06-06-2007
java - 02 July 2007 01:37 AM

I tried to instantiate CI but it still generate an error. I dont know it’s not understand my query command.

Make sure you are loading the Active Record class. In the database configuration file located at:

application/config/database.php

make sure the following variable is set to TRUE:

$db['default']['active_r'TRUE

where ‘default’ is the name of your active database group.

 Signature 

——————————————————————————————————————————————
Owan Hunte - Owner & Lead Web Architect
iSphere Technologies - Barbados-based Web Development, Hosting, SEO and E-Commerce
.(JavaScript must be enabled to view this email address)
http://ispheretechnologies.com/
——————————————————————————————————————————————

Profile