CrazyT Posted May 20, 2009 Posted May 20, 2009 How would i connect to 2 mysql databases from the same php script instead of writing the connection twice? Quote
Lithium Posted May 20, 2009 Posted May 20, 2009 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) Quote
CrazyT Posted May 20, 2009 Author Posted May 20, 2009 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? Quote
Floydian Posted May 20, 2009 Posted May 20, 2009 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... Quote
Lithium Posted May 20, 2009 Posted May 20, 2009 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!! Quote
Floydian Posted May 21, 2009 Posted May 21, 2009 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... Quote
CrazyT Posted May 21, 2009 Author Posted May 21, 2009 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 Quote
Floydian Posted May 21, 2009 Posted May 21, 2009 Re: Connecting to 2 MySQL databases from the same script. You're welcome ;) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.