running multiple mysql connections from localhost

sometimes you might need to connect to different db within the same app. The trick is that the username, passwd and hostname cannot be the same! The easy way out is to use username as root and password as nothing -> this will cause hours of grief.

another work around is to add another “true” parameter to mysql to indicate the connection is new.

$db1 = mysql_connect($dbhost, $dbuser, $dbpass);
$rv = mysql_select_db($dbname1, $db1);
$db2 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE);
$rv = mysql_select_db($dbname2, $db2);

Now, $db1 should have selected $dbname1, and $db2 should have selected $dbname2.

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.