Welcome Guest, Not a member yet? Register   Sign In
DSN issue
#1

[eluser]yoda.pt[/eluser]
Hi,

Can someone give me a light about this?

I'm trying to use DSN to connect to another database, and I'm not sure about what does $this->load->database funcion returns.

My code:

Code:
$remote = $row['driver'].'://'.$row['username'].':'.$row['password'].'@'.$row['hostname'].'/'.$row['database'].'?char_set=utf8&dbcollat=utf8_general_ci&cache_on=false&cachedir=';
$this->_edb = $this->load->database($remote);

This code is placed inside a model. Everything should work fine, but I can't get the variable refeering the new database pointer. Tried assigning a variavle, using his name, and nothing worked so far.

Can anyone explain what should I receive when calling $this->load->database?



Thank you for your support in advance Smile
#2

[eluser]Mirage[/eluser]
A quick peek at the user guide reveals that you must pass a second parameter TRUE to get the instance returned:

Code:
$this->_edb = $this->load->database($remote,true);
#3

[eluser]yoda.pt[/eluser]
Hi,


Thank you CI Mirage. I was already under that when you post it, but it still doesn't work.



I wonder why database function doesn't give out an error when it can't connect to the database. The only thing I get now is an error in my view regarding that function result is in a non-object. So, basically, I'm still with problems connection to the database.


Code:
$remote = $row->driver.'://'.$row->username.':'.$row->password.'@'.$row->hostname.'/'.$row->database.'?char_set=utf8&dbcollat=utf8_general_ci&cache_on=false';
$edb = $this->load->database($remote, true);

$query = $edb->get('metas');        
return $query;



Also, why isn't there a distinction between ip and port?


Thanks Smile
#4

[eluser]Mirage[/eluser]
[quote author="yoda.pt" date="1211318503"]
I wonder why database function doesn't give out an error when it can't connect to the database.
[/quote]

Try adding the db_debug option to your query parameters. Also I haven't looked at the database loader code, so I'm not certain how the boolean settings can/have to be passed in the dsn format.

Quote:Also, why isn't there a distinction between ip and port?

I won't answer the why - but you could easily add the :port to your hostname and just have it work.

Also if the dsn config doesn't do the trick, try building the config as an array (as illustrated in the user_guide) and pass that to your loader call.

Let us know what you find.

Cheers!
#5

[eluser]yoda.pt[/eluser]
Hi,

About the host:port issue, I indeed add the port to the host, so the question was just a bonus Smile



About using db_debug, I was already using it, and still no errors at all, even when using wrong data on DSN.



Finally, about using config as an array, that probably doesn't solve my problem, unless I can pull data from the database (as I am and need) and set the array inside the model I'm using.




Is that possible?



Thanks in advance Smile
#6

[eluser]Mirage[/eluser]
Not sure what you're saying - but you can get a reference to a database anywhere, including a model:

Code:
class SomeModel extends Model {

    var $edb;

    function SomeModel () {
        parent::Model();
        
        $CI=&get;_instance();

        $edbconf = array(
            'username' => $row->username,
            'password' => $row->password,
            'hostname' => $row->hostname,
            'port' => $row->port,
            'driver' => $row->driver,
            'database' => $row->database
        );

       $this->edb=$CI->load->database($edbconf,true);
    }


    function someOtherFunction() {
        $q=$this->edb->get('metas');
        // ...
        // ...
    }

}

Hope this clears it up.

Cheers!
#7

[eluser]yoda.pt[/eluser]
Hi,


Thank you, that solves my problem Smile

Have a nice day!! Smile
#8

[eluser]yoda.pt[/eluser]
Hi,

Just a small correction before someone else gets the same error:

Where we can see in CI Mirage's answer:

Code:
'driver' => $row->driver,

It should be:

Code:
'dbdriver' => $row->driver,

Otherwise you'll get an error saying that you didn't specified the driver.


Cheers, and thanks again for your time! Smile
#9

[eluser]Mirage[/eluser]
Thanks for pointing it out. It happens to me when I code in TextArea. ;-)




Theme © iAndrew 2016 - Forum software by © MyBB