ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

dbutil(): Call to a member function on a non-object

May 08, 2008 12:38pm

Subscribe [3]
  • #1 / May 08, 2008 12:38pm

    codex

    332 posts

    I have made a library function for backing up a database, but for some reason I’m getting ‘Call to a member function on a non-object’. If I remove ‘CI->’ I get the same error. See where the comment is. Anyone got an idea why this happens?

    class System {
        
        var $CI;
        
        /**
         * Constructor
         *
         */    
        function System()
        {    
            $this->CI =& get_instance();
            log_message('debug', "System Class Initialized");
        }
    
    function backupDB($db_backup_path='./backups_db/', $db_name='dbname') 
        {
            // Load the DB utility class
            $this->CI->load->dbutil();
            
            $time = date('d-m-Y_H-i-s');
            $filename = $db_name.'_'.$time;
            
            $prefs = array(    'tables'        => array(),
                            'format'        => 'zip',             // gzip, zip, txt
                            'filename'        => $filename .'.sql',   // File name - NEEDED ONLY WITH ZIP FILES
                            'add_drop'        => FALSE,              // Whether to add DROP TABLE statements to backup file
                            'add_insert'    => TRUE,              // Whether to add INSERT data to backup file
                            'newline'        => "\n"               // Newline character used in backup file
                          );
            
            $do = $this->CI->dbutil->backup($prefs); // <-- SCRIPT CHOKES HERE
            
            // Backup your entire database and assign it to a variable
            $backup =& $do;
            
            // Load the file helper and write the file to your server
            $this->CI->load->helper('file');
            
            if (write_file($db_backup_path . $filename .'.sql.zip', $backup)) 
            {
                return TRUE;
            }
        }
    }
  • #2 / May 08, 2008 7:44pm

    gtech

    824 posts

    is dbutil a custom library you are loading?

    have you tried

    $this->CI->load->library(dbutil);

    ?

  • #3 / May 08, 2008 7:54pm

    codex

    332 posts

    is dbutil a custom library you are loading?

    have you tried

    $this->CI->load->library(dbutil);

    ?

    Nope, it’s a CI database utility class:

    http://ellislab.com/codeigniter/user-guide/database/utilities.html

  • #4 / May 09, 2008 6:37pm

    marcmesa

    7 posts

    I tested your library on a local server, under windows with PHP4 and PHP5 and it seems to be working ok (using realpath in the write_file function).

    Are you using PHP4 or PHP5?
    In the user guide (Creating Libraries) there is an important note about get_instance() that may be helpfull:

    If you are running PHP 4 it’s usually best to avoid calling get_instance() from within your class constructors. PHP 4 has trouble referencing the CI super object within application constructors since objects do not exist until the class is fully instantiated.

  • #5 / Jun 27, 2008 8:23pm

    amw_drizz

    14 posts

    Hello,

    I am having the same problem with one of my library files as well.  I am using Apache 2.2.3 / PHP 5.2.5

    I Keep getting

    Fatal error: Call to a member function userdata() on a non-object in C:\WebServer\fqxxnlyw\public_html\system\application\libraries\Messaging.php on line 20

    This is the problem Line

    $uid = $CI->session->userdata('uid');

    And my Constructor

    function __construct(){
            $CI =& get_instance();
            $CI->load->database();    
            $CI->load->library('parser');
            $CI->load->helper('url');
            $CI->load->library('session');
            $CI->load->library('user_agent');
            $CI->load->library('validation');
            $CI->load->library('encrypt');
            $CI->load->library('email');
            $CI->load->helper(array('form', 'url'));
        }

    Any ideas of what I could do to fix the problem or do I need to put the contents of my constructor in to every function for it to load?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases