I am developing a simple app with mysql
database with one table. I update local database daily. But every week I want to update it to remote mysql database by clicking a button. I know about the synchronization and replication in PHPMyAdmin. But I don't want to use it.
I successfully checked the remote and local database connection from a file.But unable to get solution of synchronizing. Is there any method to do synchronization in PHP?
There are many articles I have read but all articles are giving methods using replication and synchronization. As I have to deliver this app to a client. I want to give him the simplest solution.
*I have cPanel,PHPMyAdmin access to remote database. Thanks in advance.
check this http://www.heidisql.com/ using this software if you update in your local system it will automatically update in your remote server.
If you're going to update a remote database I would do it in real-time rather than just once a week. It gives you a real-time, offsite backup and it's also easier for you to code.
In PHPMyAdmin you can create a new MySQL user who has rights to your remote database. It's better to create a user account that has ONLY the privileges required to update your remote database rather than using the "root" MySQL user which has all privileges.
Then you just modify your existing PHP files and everywhere you execute SQL against the local database you add a command to execute the same SQL against the remote database. For example:
(this code is untested, may contain syntax/logic errors)
<?php
$local_dsn = 'mysql:host=localhost;dbname=my_database';
$local_username = 'my_local_username';
$local_password = 'my_local_password';
$remote_dsn = 'mysql:host=remote_ip_address;dbname=my_database';
$remote_username = 'my_remote_username';
$remote_password = 'my_remote_password';
$local = new PDO($local_dsn, $local_username, $local_password);
$remote = new PDO($remote_dsn, $remote_username, $remote_password);
$sql = 'UPDATE my_table SET column1 = ? WHERE id = ?';
try{
$local_stmt = $local->prepare($sql);
$local_stmt->bindValue(1, $column1);
$local_stmt->bindValue(2, $id);
if ($local_stmt->execute() && 0 < $local_stmt->rowCount()){
$remote_stmt = $remote->prepare($sql);
$remote_stmt->bindValue(1, $column1);
$remote_stmt->bindValue(2, $id);
$remote_stmt->execute();
}
}catch(PDOException $e){
echo "An error occurred processing sql statement ($sql):" . $e->getMessage();
exit;
}
?>
It's difficult to tell if you are able to access your remote database from your local Windows machine or not. You say "I successfully checked the remote and local database connection from a file." which to me implies that you CAN access the remote database from your local machine but then you say "I have cPanel, PHPMyAdmin access to remote database" which implies you do not have access to the remote database from your local machine...
If for some reason you cannot access your remote database from your local machine you'll need to create at least one PHP page on the remote server that can execute the same SQL queries that you're executing locally. You'll want to use a secure connection (HTTPS) and pass some kind of authentication credentials (at the very least some kind of hash or encrypted key) along with the SQL to be executed.