CodeIgniter Forums
Connecting to second db issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Connecting to second db issue (/thread-10804.html)

Pages: 1 2


Connecting to second db issue - El Forum - 08-13-2008

[eluser]Bramme[/eluser]
Hey all.

I need to connect to a second db (the phpbb db) to fetch a list of recent topics.

I use this code:
Code:
$dsn = 'mysql://user:[email protected]/phpbb3';
$con = $this->load->database($dsn, TRUE);

$con->select('phpbb3_topics.*');
$con->join('phpbb3_posts', 'phpbb3_posts.topic_id = phpbb3_topics.topic_id');
$con->order_by('phpbb3_posts.post_time', 'desc');
$query = $con->get('phpbb3_topics', 4, 0);
foreach($query->result() as $row) {
    $url = "/forum/viewtopic.php?f={$row->forum_id}&t;={$row->topic_id}";
    $top[$url] = $row->topic_title;    
}
However I get a "Call to a member function result() on a non-object on line x" Line x is where the foreach loop is...


Connecting to second db issue - El Forum - 08-13-2008

[eluser]Hannes Nevalainen[/eluser]
I'm not sure you can access your database with a dsn,, Setup a second connection in your config/database.php =)

DB connection in user guide

Happy Coding =)


Connecting to second db issue - El Forum - 08-13-2008

[eluser]Bramme[/eluser]
From that same page...
Quote:Or you can submit your database values as a Data Source Name. DSNs must have this prototype:
$dsn = 'dbdriver://username:[email protected]/database';

$this->load->database($dsn);



Connecting to second db issue - El Forum - 08-13-2008

[eluser]Hannes Nevalainen[/eluser]
Srry, My bad.
Try to
Code:
echo '<pre>',print_r($query),'</pre>';
before you do ->result().

Post the results please, (if it doesn't help ypu)


Connecting to second db issue - El Forum - 08-13-2008

[eluser]Bramme[/eluser]
Code:
echo "query: <pre>";
    print_r($query);
    echo "</pre>";
Results in
Code:
query: <pre></pre>
in the source code...


Connecting to second db issue - El Forum - 08-13-2008

[eluser]Bramme[/eluser]
Okay, nevermind. it's clearly a bug, because when I connect using a different group in the database config file, everything works.


Connecting to second db issue - El Forum - 08-13-2008

[eluser]Hannes Nevalainen[/eluser]
look at the top in sourece (the print_r()) echoes it's result, to ,make it return you have to provide the second parameter bool:TRUE).

print_r($query,true);

I forgot this (like always)


Connecting to second db issue - El Forum - 08-13-2008

[eluser]xwero[/eluser]
My guess is if you load a connection string the AR library isn't available. If that is the problem it should be mentioned somewhere in the documentation and also a way to make the AR library available working with connection strings.

You could try
Code:
$query = $this->db->query('SELECT phpbb3_topics.* FROM phpbb3_topics JOIN phpbb3_posts ON phpbb3_posts.topic_id = phpbb3_topics.topic_id LIMIT 4');
To check if it is the AR setting that is missing.


Connecting to second db issue - El Forum - 08-13-2008

[eluser]Bramme[/eluser]
Okay, this is even more annoying.

I can succesfully connect to the second database. Though when I do that, even if I use mysql_close(), the database library uses the second database to do all my other queries. Offcourse I end up with an sql error then because it can't find the tables.


Connecting to second db issue - El Forum - 08-13-2008

[eluser]xwero[/eluser]
Have you set the pconnect setting to false?