Welcome Guest, Not a member yet? Register   Sign In
Connecting to second db issue

Hey all.

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

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

$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...

[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 =)

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';


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

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

echo "query: <pre>";
    echo "</pre>";
Results in
query: <pre></pre>
in the source code...

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

[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).


I forgot this (like always)

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
$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.

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.

Have you set the pconnect setting to false?

Theme © iAndrew 2016 - Forum software by © MyBB