简体   繁体   English

如何使用PHP将SQL Server表中新添加的行转移到相同的MySQL表中?

[英]How do I transfer newly added rows in an SQL Server table to an identical MySQL table using PHP?

Basically, I have two identical tables in SQL Server and MySQL. 基本上,我在SQL Server和MySQL中有两个相同的表。 I want to use PHP in such a way that I'll only have to manually insert new values in one of them. 我想以这样一种方式使用PHP,我只需要在其中一个中手动插入新值即可。 I want to make a PHP code where the newly inserted values in the SQL Server table will also be inserted in its identical counterpart in MySQL with the press of a button. 我想制作一个PHP代码,其中只要按一下按钮,SQL Server表中新插入的值也将插入MySQL中相同的副本中。

For example, I have a table named "Customers" in both SQL Server and MySQL with the rows "ID (auto incremented)", Name, and Address. 例如,我在SQL Server和MySQL中都有一个名为“ Customers”的表,其中的行为“ ID(自动递增)”,“名称”和“地址”。 I insert new values into those columns in SQL Server. 我将新值插入SQL Server的这些列中。 How do I make it so that I'll only have to press a button made in PHP so that I won't have to do the whole "insert into" process again in MySQL? 我如何做到这一点,以便只需要按PHP中的按钮,就不必在MySQL中再次执行整个“插入”过程了?

Any ideas are much appreciated! 任何想法都非常感谢!

According to new information given in the comments, I'm changing my answer and adjusting the code. 根据评论中提供的新信息,我正在更改答案并调整代码。

Code example: 代码示例:

<?php
 $serverName = "server"; //serverName\instanceName
 $connectionInfo_mssql = array("Database"=>"DB", "UID"=>"username", "PWD"=>"password","CharacterSet"=>"UTF-8");
 $conn_mssql = sqlsrv_connect($serverName, $connectionInfo_bi);
 $conn_mysql = new mysqli("server", "username", "password", "db");

//SELECT FROM MS SQL DB
$mssql_array = array();
$ms_sql = "SELECT column_names FROM db_table";
$mssql_query = sqlsrv_query($conn_mssql , $ms_sql);
while($row = sqlsrv_fetch_array($mssql_query) {
  $mssql_array[] = array('name' => $row['name'],
                         'adress' => $row['adress']);
}

foreach($mssql_array as $key => $value) {

   //SELECT FROM MySQL DB
   $my_sql = "SELECT column_names FROM db_table WHERE name = '".$value['name']."' AND adress = '".$value['adress']."'";
   $mysql_query = mysqli_query($conn_mysql , $my_sql);
   $num_rows = mysqli_num_rows($mysql_query);

   if ($num_rows == 0) {
     //Insert in MySQL DB
     $sql = "INSERT INTO db_table (db_columns) VALUES (variables_from_array)";
     $sql_query = mysqli_query($conn_mysql, $sql);
   } else {
     //There is a record in DB, and maybe you want to update it. If not, then lose this else part.
   }

}

echo 'Table Customers from MS SQL DB and table Customers from MySQL DB are now synced!';

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM