How can i synchronize two database tables with PHP?

后端 未结 8 611
萌比男神i
萌比男神i 2021-01-01 04:56

I need to use PHP to copy data from one MySQL database to another.

I can build and array of all the values to go into the other database but first I want to make su

8条回答
  •  失恋的感觉
    2021-01-01 05:47

    Probably the easiest way to do this would be

    $sql = "SELECT * FROM tableA WHERE 1"
    
    $results = mysql_fetch_assoc($sql);
    
    $sql = "truncate table tableB";
    
    // run truncate
    
    foreach($result as $update){
    
       $sql = "Insert into table b VALUES(....)"
    
       // run insert
    }
    

    But you need to be extremely careful here. Make sure that the only process that can write to tableB is the one that does the copy from tableA other wise you will have lost data. Also make sure that nothing can write to tableA once this process has begun.

    The best practice for this would be to not do this in php but rather through mysql replication.

提交回复
热议问题