Jump to content

uploading to 2 separate SQL databases on different servers using PHP


inyachtcrew

Recommended Posts

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

Link to comment
Share on other sites

 

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.