Welcome Guest, Not a member yet? Register   Sign In
How do I access the database from a library class?
#4

[eluser]hal10001[/eluser]
[quote author="hal10001" date="1233746328"]I created my own library class, which works fine, but I just can't get connected to the database to run a query. From a function I try to load in the "database" library, but get the following:

Unable to load the requested class: database

This is the basic code:

Code:
class MyClass {

    function myFunction()
    {
        $CI =& get_instance();
        $CI->load->library( 'database' );

        $query = $this->db->query( 'SOME QUERY GOES HERE' );
        return count( $query->result() );
    }
    
}

How do I do this from a library class, and do I need a model, or can I just run the query like normal when using the database class? Thanks![/quote]

Thank you for your help on this. It was a combination of things that you both mentioned. I ended up with the following:

Code:
class MyClass {

    function myFunction()
    {
        $CI =& get_instance();
        $db = $CI->load->database( 'default', TRUE );

        $query = $db->query( 'SOME QUERY GOES HERE' );
        return count( $query->result() );
    }
    
}


Messages In This Thread
How do I access the database from a library class? - by El Forum - 02-03-2009, 11:18 PM
How do I access the database from a library class? - by El Forum - 02-03-2009, 11:22 PM
How do I access the database from a library class? - by El Forum - 02-03-2009, 11:24 PM
How do I access the database from a library class? - by El Forum - 02-04-2009, 06:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB