Jump to content


Photo

uploading to 2 separate SQL databases on different servers using PHP


  • Please log in to reply
1 reply to this topic

#1 inyachtcrew

inyachtcrew

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 10 December 2013 - 07:56 PM

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.
?>



#2 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,539 posts

Posted 11 December 2013 - 10:32 AM

 

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 by Ch0cu3r, 11 December 2013 - 10:40 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com