如何使用 PHP 将 SQL 服务器 table 中新添加的行传输到相同的 MySQL table?
How do I transfer newly added rows in an SQL Server table to an identical MySQL table using PHP?
基本上,我在 SQL 服务器和 MySQL 中有两个相同的 table。我想以这样一种方式使用 PHP,我只需要在其中一个中手动插入新值。我想制作一个 PHP 代码,其中 SQL 服务器 table 中新插入的值也将通过按下按钮插入到 MySQL 中的相同对应值中。
例如,我在 SQL 服务器和 MySQL 中都有一个名为 "Customers" 的 table,其中包含行 "ID (auto incremented)"、名称和地址。我将新值插入 SQL 服务器中的那些列。我该怎么做才能让我只需要按下在 PHP 中制作的按钮,这样我就不必在 MySQL 中再次执行整个 "insert into" 过程?
非常感谢任何想法!
根据评论中给出的新信息,我正在更改我的答案并调整代码。
代码示例:
<?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!';
?>
基本上,我在 SQL 服务器和 MySQL 中有两个相同的 table。我想以这样一种方式使用 PHP,我只需要在其中一个中手动插入新值。我想制作一个 PHP 代码,其中 SQL 服务器 table 中新插入的值也将通过按下按钮插入到 MySQL 中的相同对应值中。
例如,我在 SQL 服务器和 MySQL 中都有一个名为 "Customers" 的 table,其中包含行 "ID (auto incremented)"、名称和地址。我将新值插入 SQL 服务器中的那些列。我该怎么做才能让我只需要按下在 PHP 中制作的按钮,这样我就不必在 MySQL 中再次执行整个 "insert into" 过程?
非常感谢任何想法!
根据评论中给出的新信息,我正在更改我的答案并调整代码。
代码示例:
<?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!';
?>