Forum Moderators: coopster
Do they allow remote connections or are they locked down to local connections?
I've run into that issue in the past where some of the db servers allowed remote connections and some didn't. I ended up creating a script that would run on the local server and another master script that would run on the primary server. The master script would run the local scripts which would access the db's. The local scripts would then output the results which would be read by the master script.
$db = @mysql_connect (host , user , passwd);
$db2 = @mysql_connect (host2 , user , passwd);
$db3 = @mysql_connect (host3 , user , passwd);
etc...
then you can make your queries like this:
$result = db_query($sql, $db1)
or
$result = db_query($sql, $db2)
...
go to php.net and check out all available [my]Sql functions php has to offer.
$db_host = "site";
$db_username = "username";
$db_password = "pass";
$db_name = "database_name";
$conn = mysql_connect($db_host,$db_username,$db_password);
$db = mysql_select_db($db_name);
============================
$db1_host = "site";
$db1_username = "username";
$db1_password = "pass";
$db1_name = "database_name";
$conn1 = mysql_connect($db1_host,$db1_username,$db1_password);
$db1 = mysql_select_db1($db1_name);
and so on?
## SET UP CONNECTION VARIABLES $db_host = "site0"; $db_username = "username0"; $db_password = "pass0"; $db_name = "database_name0"; $db1_host = "site1"; $db1_username = "username1"; $db1_password = "pass1"; $db1_name = "database_name1"; ### MAKE CONNECTIONS $conn = mysql_connect($db_host,$db_username,$db_password); $conn1 = mysql_connect($db1_host,$db1_username,$db1_password); ### RUN QUERIES $q = mysql_db_query($db_name,$q_string,$conn); $q1 = mysql_db_query($db_name1,$q_string1,$conn1); ### USE QUERY OUTPUT while($row=mysql_fetch_array($q)) { [do something with data] } ["regular" non-db stuff, if you like] while($row=mysql_fetch_array($q1)) { [do something with data1] } etc.
IMHO, I just like to keep everything together like that. The code you posted is pretty close*, too ... it's a personal preference, mostly.
*There's no
mysql_select_db1() function, for example. You need to use either mysql_select_db($db,$resource_link_identifier) or mysql_db_query($db,$q,$resource_link_identifier) to hook up with a specific connection among many. I use mysql_db_query() in these situations because what the heck? Why not do the query if you're selecting the db? <edit>Interestingly, nearly the same code (didn't see it, I swear!) can be found in the PHP manual for
section [php.net]. Scroll down to the comments section.</edit>
mysql_select_db()
$query = "SELECT id, email FROM users WHERE email='$u'";
$result = @mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
if ($row) {
$id = $row[0];
$email = $row[1];-------------------------------------------------------
// Make the query.
$query = "UPDATE users SET password='".md5($p)."' WHERE id='$id'";
resource mysql_query [php.net] ( string query [, resource link_identifier] )
Example:
$conn = @mysql_connect($server, $user, $password);
$db = @mysql_select_db($database, $conn);
$query = "SELECT id, email FROM users WHERE email='$u'";
$result = @mysql_query ($query, $conn);
if ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
$id = $row[0];
$email = $row[1];
//
// more code here
//
}