Jump to content
MakeWebGames

Connecting to 2 MySQL databases from the same script.


Recommended Posts

Posted

Re: Connecting to 2 MySQL databases from the same script.

Basically, you need to create a second mysql connection and explicitly tell each mysql function which one to use.

$con1 = mysql_connect( 'host','user','pass' );

$con2 = mysql_connect( 'host','user','pass' );

//Use first connection

mysql_select_db( "db1", $con1 );

//Use second connection

mysql_select_db( "db2", $con2 );

//Query first connection (db1)

mysql_query( "SELECT etc....", $con1 );

//Query second connection (db2)

mysql_query( "SELECT etc....", $con2 );

As of the "style" on MCC v1.* using the $c for the connections, you'd be using $con1 (for one db) and $con2 (for the other)

Posted

Re: Connecting to 2 MySQL databases from the same script.

Yeah, i knew that way was just wondering if i could just put it into 1 hmmm.. I think mysqli does what i need *me thinks?

Posted

Re: Connecting to 2 MySQL databases from the same script.

If both databases (or however many dbs...) have the same user, then you can append the db name to the table names for "on the fly" access to both dbs.

Of course you can select a db to use as well.

Hope that helps...

Posted

Re: Connecting to 2 MySQL databases from the same script.

 

If both databases (or however many dbs...) have the same user, then you can append the db name to the table names for "on the fly" access to both dbs.

Of course you can select a db to use as well.

Hope that helps...

Hehe i thought of that option too, but turns out more messy!!

Posted

Re: Connecting to 2 MySQL databases from the same script.

In case one wanted to know how to do that:

select users.userid, stats.someStat from main_db.users

left join secondary_db.stats on users.userid = stats.userid

 

If you have two tables: users and stats, in the databases: main_db and secondary_db respectively, the above query will get the two tables from the two db's in one query.

 

select column from db.table, db.table

That is the generic syntax...

Posted

Re: Connecting to 2 MySQL databases from the same script.

 

In case one wanted to know how to do that:

select users.userid, stats.someStat from main_db.users

left join secondary_db.stats on users.userid = stats.userid

 

If you have two tables: users and stats, in the databases: main_db and secondary_db respectively, the above query will get the two tables from the two db's in one query.

 

select column from db.table, db.table

That is the generic syntax...

Thanks!! :), and for that ill give you +1, witch i neva do lol

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...