Part of the EllisLab Network
   
 
Cannot query database in custom_helper?
Posted: 30 June 2007 03:50 AM   [ Ignore ]  
Summer Student
Total Posts:  28
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 09:16 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  169
Joined  08-22-2006
java - 30 June 2007 03: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 08:37 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  28
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 10:09 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  61
Joined  06-06-2007
java - 01 July 2007 08:37 PM

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 - Founder, Lead Web Architect
iSphere Technologies - e-Business Consulting, Development and Support

http://ispheretechnologies.com/
——————————————————————————————————————————————

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120536 Total Logged-in Users: 41
Total Topics: 126598 Total Anonymous Users: 3
Total Replies: 665552 Total Guests: 400
Total Posts: 792150    
Members ( View Memberlist )
Newest Members:  sweeper240andrew nsuntroprachel123Rach123Glenn StavaImfaelSMS!persanulandrewknight