.
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #mysql-database-synchronization-with-php
2 |
3 | Synchronize MySQL Database from One Server to Another Server or from One Database to Another Database on the same server using Simple PHP Script.
4 |
5 | The source code is available at [github](https://github.com/bharat383/mysql-database-synchronization-with-php.git). You can either clone the repository or download a zip file of the latest release.
6 |
7 | ## Requirement
8 |
9 | 1. cURL should be enabled
10 |
11 | ## How To Use
12 |
13 | 1. copy & paste `server1` directory files on your first server.
14 |
15 | 2. set database details for the database which you want to synchronize to another database in
16 | `includes/config.php` file.
17 |
18 | 3. copy & paste `server2` directory files on your second server.
19 |
20 | 4. set Synchronization URL in `includes/config.php` which should have `server2` directory files.
21 |
22 | 5. set database details for the database where you want to synchronize first database data.
23 |
24 | 6. execute `server1/example.php`
25 |
26 | ## Example
27 |
28 | `server1/example.php` (Send Data to server2/example.php)
29 |
30 | To make it automatic synchronization on particular interval, you can run the
31 | server1/example.php as cronjob on specific time.
32 |
33 | ## Output
34 |
35 |
36 | ``` php
37 | Array
38 | (
39 | [data] => Array
40 | (
41 | [bh_user_master] => Table Created. 8 Rows Inserted.
42 | [bh_user_message] => Table Created.
43 | )
44 |
45 | [status] => success
46 | [message] => Database synchronization initiated.
47 | )
48 |
49 |
50 | Array
51 | (
52 | [data] => []
53 | [status] => fail
54 | [message] => No Table Found./Table Structure Not Found./
55 | )
56 |
57 | ```
58 |
59 | ## Limitation :
60 |
61 | 1. This is just for the Development mode only demo. When the script
62 | synchronize the database, first it will drop the table on second
63 | server and create new table and after that insert all data.
64 | During the synchronization time, f the server stopped to respond,
65 | the second server database will be truncated.
66 |
67 |
68 | 2. Synchronization time, table structure and all rows will be
69 | synchronization to avoid the collumn/data updation after
70 | last synchronization.
71 |
72 |
73 | ## Author
74 |
75 | Bharat Parmar
76 |
77 | Ahmedabad, India.
78 |
79 | Mobile : +91 9687766553
80 | Email : bharatparmar383@gmail.com
81 |
82 |
--------------------------------------------------------------------------------
/server1/class/server1.class.php:
--------------------------------------------------------------------------------
1 | dbconnection();
17 | }
18 |
19 | private function dbconnection(){
20 | $this->con = mysqli_connect(HOST,DBUSER,DBPASSWORD,DBNAME) or mysqli_connect_error();
21 | }
22 |
23 | public function getdbupdate(){
24 | $table_list = $this->table_list();
25 |
26 | if(count($table_list)>0){
27 | foreach ($table_list as $key => $table_name) {
28 | $response_array['data'][$table_name] = $this->synch_table_data($table_name);
29 | }
30 | $response_array['status']="success";
31 | $response_array['message']="Database synchronization initiated.";
32 |
33 | return $response_array;
34 | exit;
35 | } else {
36 | $response_array['status']="fail";
37 | $response_array['message']="No Tables Found.";
38 | $response_array['data']=array();
39 | return $response_array;
40 | exit;
41 | }
42 | }
43 |
44 | private function table_list(){
45 | $table_query = mysqli_query($this->con,"SHOW TABLES");
46 | $table_array = array();
47 |
48 | if(mysqli_num_rows($table_query)>0){
49 | while($table_query_data = mysqli_fetch_assoc($table_query)){
50 | $table_array[] = $table_query_data['Tables_in_'.DBNAME];
51 | }
52 | }
53 | return $table_array;
54 | }
55 |
56 | private function synch_table_data($table_name){
57 | $table_check_query = mysqli_query($this->con,"show create table `".$table_name."`");
58 | if(mysqli_num_rows($table_check_query)>0){
59 | $table_structure_string = mysqli_fetch_assoc($table_check_query);
60 | $table_structure_string = $table_structure_string['Create Table'];
61 | $table_structure_string = str_replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS", $table_structure_string);
62 |
63 | $post_data['table_name'] = $table_name;
64 | $post_data['table_structure'] = $table_structure_string;
65 | $table_query = mysqli_query($this->con,"select * from `".$table_name."`");
66 | if(mysqli_num_rows($table_query)>0){
67 | while($table_data = mysqli_fetch_assoc($table_query)){
68 | $post_data['table_data'][] = $table_data;
69 | }
70 | }
71 | //SYNC DATA TO LIVE SERVER
72 | $ch = curl_init(SYNC_URL);
73 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
74 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
75 | // execute!
76 | $response = curl_exec($ch);
77 | // close the connection, release resources used
78 | curl_close($ch);
79 |
80 | return $response;
81 | } else {
82 | return 'Table Structure Not Found.';
83 | }
84 | }
85 |
86 | public function __destruct(){
87 | mysqli_close($this->con);
88 | }
89 | }
90 | ?>
91 |
--------------------------------------------------------------------------------
/server1/example.php:
--------------------------------------------------------------------------------
1 | getdbupdate();
15 |
16 | echo "";
17 | print_r($output);
18 | echo "
";
19 |
20 | ?>
--------------------------------------------------------------------------------
/server1/includes/config.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/server2/class/server2.class.php:
--------------------------------------------------------------------------------
1 | dbconnection();
17 | }
18 |
19 | private function dbconnection(){
20 | $this->con = mysqli_connect(HOST,DBUSER,DBPASSWORD,DBNAME) or mysqli_connect_error();
21 | }
22 |
23 | public function table_update(array $post_array){
24 |
25 | $table_name = @$post_array['table_name'];
26 | $table_structure = @$post_array['table_structure'];
27 | $table_data = @$post_array['table_data'];
28 |
29 | //DROP CURRENT TABLE
30 | mysqli_query($this->con,"drop table `".$table_name."`");
31 |
32 | //CREATE TABLE WITH NEW STRUCTURE
33 | mysqli_query($this->con,$table_structure);
34 |
35 | //INSERT TABALE DATA
36 |
37 | if(count($table_data)>0){
38 | $collumn_query = mysqli_query($this->con,"SHOW COLUMNS FROM `".$table_name."`");
39 | if(mysqli_num_rows($collumn_query)>0){
40 | $data_insert_query = "INSERT INTO `".$table_name."` ( ";
41 |
42 | while($collumn_data = mysqli_fetch_assoc($collumn_query)){
43 | $data_insert_query.="`".$collumn_data['Field']."`, ";
44 | }
45 | $data_insert_query=trim($data_insert_query,", ")." ) VALUES ";
46 | $insert_query = $data_insert_query;
47 |
48 | foreach ($table_data as $key => $value) {
49 | $insert_query.=" ('".implode("', '",$value)."'),";
50 |
51 | if($key>0 && $key%QUERY_LIMIT==0){
52 | $insert_query = trim($insert_query,",");
53 | mysqli_query($this->con,$insert_query);
54 | $insert_query=$data_insert_query;
55 | }
56 | }
57 |
58 | if($insert_query!=""){
59 | $insert_query = trim($insert_query,",");
60 | mysqli_query($this->con,$insert_query);
61 | }
62 | return "Table Created. ".mysqli_affected_rows($this->con)." Rows Inserted.";exit;
63 |
64 | } else {
65 | return "Table Collumn Not Found.";exit;
66 | }
67 | } else {
68 | return "Table Created.";exit;
69 | }
70 | }
71 |
72 | public function __destruct(){
73 | mysqli_close($this->con);
74 | }
75 | }
76 | ?>
77 |
--------------------------------------------------------------------------------
/server2/example.php:
--------------------------------------------------------------------------------
1 | table_update($_POST);
18 | print_r($output);
19 | exit;
20 | } else {
21 | echo "Parameter Missings.";
22 | exit;
23 | }
24 |
25 | ?>
--------------------------------------------------------------------------------
/server2/includes/config.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------