CodeIgniter Forums

Full Version: [SOLVED] library question so simple/noob you'll be proud to answer me ...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Dagobert Renouf[/eluser]
Hi guys,

My library :

Code:
public function __construct()
    {

        $CI =& get_instance();
        $CI->load->helper('url');
        $CI->load->database();

    }

    public function display($table, $params = "")
    {
        if ($this->db->table_exists($table))
        {

            echo 'a';

        }

    }

it says db is not defined, why is that ? (I load the database in the constructor)

El Forum

[eluser]Seppo[/eluser]
CI does not attach the db object to all libraries... you should use
Code:
$CI =& get_instance();
        if ($CI->db->table_exists($table))

El Forum

[eluser]ontguy[/eluser]
I think it should setup like this:

Code:
var $CI;

public function __construct()
    {

        $this->CI =& get_instance();
        $this->CI->load->helper('url');
        $this->CI->load->database();

    }

    public function display($table, $params = "")
    {
        if ($this->CI->db->table_exists($table))
        {

            echo 'a';

        }

    }

El Forum

[eluser]Dagobert Renouf[/eluser]
ahah I thought it would be simpler than that :

Seppo : same error

ontguy : "Call to a member function table_exists() on a non-object"

El Forum

[eluser]ontguy[/eluser]
What version of php are you working with?

El Forum

[eluser]Dagobert Renouf[/eluser]
PHP Version 5.2.6

El Forum

[eluser]ontguy[/eluser]
Does anything from the url helper work in that function? try
Code:
echo base_url();

If it works, the CI reference was passed successfully to the library.

El Forum

[eluser]Dagobert Renouf[/eluser]
yes it does work

El Forum

[eluser]xwero[/eluser]
Try loading the database in the method

El Forum

[eluser]Dagobert Renouf[/eluser]
still the same :-s

Call to a member function database() on a non-object
Pages: 1 2