inyachtcrew Posted December 11, 2013 Share Posted December 11, 2013 I am trying to add in my php script named mysql_connect.php a way to send data to two separate databases at one time with the same scrip. I have tried many different ways to try this, and it either runs the error script or uploads successfully to only one of the two databases. Below is the script if anyone can help. Is there a way to make two database connections with one script? <?php DEFINE ('DB_USER', 'user');DEFINE ('DB_PASSWORD', 'password');DEFINE ('DB_HOST', 'my.server.name.com');DEFINE ('DB_NAME', 'name');// Make the connection.$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL this time: ' . mysql_error() );// Select the database.@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );// Create a function for escaping the data.function escape_data ($data) { // Address Magic Quotes. if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } // Check for mysql_real_escape_string() support. if (function_exists('mysql_real_escape_string')) { global $dbc; // Need the connection. $data = mysql_real_escape_string (trim($data), $dbc); } else { $data = mysql_escape_string (trim($data)); }if (function_exists('mysql_real_escape_string')) { global $other; // Need the connection. $data = mysql_real_escape_string (trim($data), $other); } else { $data = mysql_escape_string (trim($data)); } // Return the escaped value. return $data;} // End of function.?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted December 11, 2013 Share Posted December 11, 2013 (edited) I am trying to add in my php script named mysql_connect.php a way to send data to two separate databases at one time with the same scrip. Why do you need separate databases? Having multiple connections can over complicate things. To answer you question yes you can connect different servers at the same time. You need to call mysql_connect for each server // first server connection $connection_1 = mysql_connect('hostname1', 'username', 'password'); // select database on first server mysql_select_db('database_one', $connection_1); // second server connection $connection_2 = mysql_connect('hostname2', 'username', 'password'); // select database on second server mysql_select_db('database_two', $connection_2); You'll will then need to pass the connection resource ($connection_1 or $connection_2) when a mysql_* function optionally requires it. That way PHP knows which database you want to interact with. For example when querying the database(s) you need to pass the connection resource as the second argument // query database 1 $db1_result = mysql_query(/* the sql query for database 1*/, $connection_1); // query database 2 $db2_result = mysql_query(/* the sql query for database 2*/, $connection_2); Edited December 11, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
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.