我有两个数据库:
两个数据库都有相同的表:
数据库名称:首先
tables:
1. area
2. group
数据库名称:第二个
tables:
1. area
2. group
所以我想用php同时将记录保存到两个数据库中。
答案 0 :(得分:0)
2个数据库意味着两个独立的数据库服务器 如果是这样,请进行2个单独的连接,然后相应地进行插入。
使用mysqli,它看起来像这样
//Connection to database 1
$con1=mysqli_connect("xxxxx:portno","database","username","password");
//Connection to database 2
$con2=mysqli_connect("xxxxx:portno","database","username","password");
$sql_table1= "INSERT INTO table_name VALUES (value1, value2, value3, ...)";
$sql_table2= "INSERT INTO table_name VALUES (value1, value2, value3, ...)";
if ($con1->query($sql_table1) === TRUE) {
echo "Values inserted successfully in table 1 of db1";
}
else {
echo "Error in insertion";
}
if ($con2->query($sql_table2) === TRUE) {
echo "Values inserted successfully in table 2 of db2";
}
else {
echo "Error in insertion";
}