Recent Topics

1 Jan 10, 2007 01:53    

My site requires that I connect to another database to display certain content at the top of the site from this other db. I've modified the skin to do that, by using b2evolution's DB class to change to a different DB:



//connect to other db
$db_config = array(
	'user'          => $mosConfig_user,     // your MySQL username
	'password'      => $mosConfig_password,     // ...and password
	'name'          => $mosConfig_db,  // the name of the database
	'host'          => $mosConfig_host,    // MySQL Server (typically 'localhost')
);

$DB = & new DB( $db_config );

//do whatever html is necessary here

//include the b2 config info
include("conf/_basic_config.php");

//connect to b2 db
$DB = & new DB( $db_config );

The first half of it works wonderfully, but the reconnecting back to the b2 database isn't working. I'm getting a bunch of errors, that look like so:

Table 'blog_dev.T_blogs' doesn't exist(Errno=1146)
Table 'blog_dev.T_sessions' doesn't exist(Errno=1146)

Those tables certainly do exist, as the blog was working before I started mucking about with this ;)

From what I can tell, it seems to be ignoring the $tableprefix -- it should be doing blog_dev.evo_blogs, not blog_dev.T_blogs. Any guesses on why, and how to fix this?

I really need to make this work.

2 Jan 10, 2007 22:31

You're missing the $db_config settings from /conf/_advanced.php.

Just do it like this:


//connect to other db 
 $temp_db_config = array( 
    'user'          => $mosConfig_user,     // your MySQL username 
    'password'      => $mosConfig_password,     // ...and password 
    'name'          => $mosConfig_db,  // the name of the database 
    'host'          => $mosConfig_host,    // MySQL Server (typically 'localhost') 
 ); 
 
 $DB = & new DB( $temp_db_config ); 
 
 //do whatever html is necessary here 

 //connect to b2 db again:
 $DB = & new DB( $db_config );

3 Jan 10, 2007 23:46

Hey blueyed, what is the point of using a & on the line;

$DB = & new DB( $temp_db_config ); 

4 Jan 11, 2007 01:24

php4 legacy to use a reference to the created object not a copy. In PHP5, the "&" is implied (always a reference).

5 Jan 11, 2007 02:14

Just as an aside, would it not be better to do $temp_DB = & new DB( $temp_config ); so there's no chance of "forgetting" to reconnect to the normal db?

¥

6 Jan 11, 2007 02:33

Yes, it's probably the best practise to use $temp_DB also and leaving the original $DB object alone.

However, this depends on "//do whatever html is necessary here" - there might be a function which uses the $DB global, but that's unlikely.

In any case, the original $DB should get saved (and restored later), instead of reconnecting again.


Form is loading...