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

[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:pass@localhost/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...
#2

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

[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:password@hostname/database';

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

[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)
#5

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

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

[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)
#8

[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.
#9

[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.
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB