sql 2 sql

By zenx on Feb 26, 2011

What this does is it grabs all items from local table and then inserts them on a remote table, this is supposed to be run at terminal.

<?php

// Connecting to a local database
$con = mysql_connect("localhost","username","password");
mysql_select_db("dbname", $con);

// Selecting all items that are going to be sent to a 2nd server from local table
$result = mysql_query("SELECT * FROM table1");

$count = 0;

while($row = mysql_fetch_array($result)) {

    // Cleaning the variables
    unset($rowone);
    unset($rowtwo);
    unset($rowthree);
    unset($rowfour);

    // Setting the variables
    $rowone = $row['rowone'];
    $rowtwo = $row['rowtwo'];
    $rowthree = $row['rowthree'];
    $rowfour = $row['rowfour'];

    // Connecting to a remote server and choosing the database
    $conx = mysql_connect("my.2nd.server","username","password");
    mysql_select_db("dbname", $conx);

    // Creating and executing the query
    $query = mysql_query("INSERT INTO remotetable1 (rowone, rowtwo, rowthree, rowfour) VALUES ('" . $rowone . "', '" . $rowtwo . "', '" . $rowthree . "', '" . $rowfour . "')");

    // Closing the connection to the remote server
    mysql_close($conx);

    // Displaying it nicely on the terminal
    $count++;
    echo "$count    -   $rowone -   $rowtwo -   $rowfour \n";

}

mysql_close($con);

?>

Comments

Sign in to comment.
Hawkee   -  Feb 27, 2011

I don't think this is necessary. You can do a mysqldump, connect to the remote server with mysql -h and do "load data local infile" to send the local dump to the remote server.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.