Last Updated: September 09, 2019
·
442
· jammer9

Upload the records from localhost server to remote server

I was working on a project where I had to push/upload the data from localhost server(using XAMPP) to remote server.
So basically here I had to connect two databases on different servers. Records had to be fetched from one database hosted on localhost server and then inserted into other database hosted on the remote server.

The solution :

The basic requirement is the availability of remote SQL connection.

NOTE : Its available on all premium hosting services. http://heliohost.org/ provides remote connection on free hosting.

Making connections to the database. Here there are two databases on different server, so two connections are established.


$conn1 = mysqlconnect($hostname, $username, $password);
mysqlselectdb('db1', $conn1);
</code></pre>

Here there is a need to add 4th parameter in the second connection.

$conn2 = mysqlconnect($hostname2, $username2, $password2, true);
mysqlselectdb('db2', $conn2);
</code></pre>

Add the connection variable with the query.

Fetching the records from table1 in db1.

$query1="SELECT * FROM table1";
$res=  mysqlquery($query1,$conn1);
$result=mysqlfetch_row($res);
</pre></code>

Now, insert the fetched record into table2  of db2.

$query2 = "INSERT into db2.table2 (col1,col2) values ($result[0],$result[1])";
mysqlquery($query2, $conn2);
</pre></code>

Its done.