[英]Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL
I have to select data from a table in a database and insert them into another table in a different database.我必须从数据库中的表中选择数据并将它们插入到不同数据库中的另一个表中。 The below code returns the
$select_query
OK, but the $insert_query
is not OK.下面的代码返回
$select_query
OK,但$insert_query
不正确。 Could you please correct the code and let me know your response?您能否更正代码并让我知道您的回复?
$host1 = "localhost";
$user1 = "jackpot";
$pass1 = "jackpot";
$db1 = "pinkapple";
$host2 = "localhost";
$user2 = "jackpot";
$pass2 = "jackpot";
$db2 = "blueberry";
$mysql_connection1 = mysql_connect($host1, $user1, $pass1);
mysql_select_db($db1, $mysql_connection1) or die(mysql_error());
$select_query = mysql_query("SELECT field1, field2, field3 FROM tree WHERE date_entered > '2014-01-01 16:22:00'", $mysql_connection1);
$number = mysql_num_rows($select_query);
if ($select_query) {
echo "Select Query is OK <br>";
echo $number ."<br>";
} else {
echo "Select Query is not OK <br>";
}
$mysql_connection2 = mysql_connect($host2, $user2, $pass2, true);
mysql_select_db($db2, $mysql_connection2) or die(mysql_error());
while ($row = mysql_fetch_array($select_query)) {
$field1 = $row['field1'];
$field2 = $row['field2'];
$field3 = $row['field3'];
$insert_query = mysql_query("INSERT INTO jungle (desk1, chair1, table1) VALUES ('$field1', '$field2', '$field3')", $mysql_connection2);
if ($insert_query) {
echo "Insert Query is OK <br>";
} else {
echo "Insert Query is not OK <br>";
}
}
mysql_close($mysql_connection1);
mysql_close($mysql_connection2);
below you can see the schematic image of the tables.下面你可以看到表格的示意图。
You need to establish the values?你需要建立的价值观?
"INSERT INTO jungle (desk1, chair1, table1) VALUES (value1, value2, value3)"
You should collect the data from the first select and then you can set the pertinent values in your insert statement您应该从第一个选择中收集数据,然后您可以在插入语句中设置相关值
You can do that using the mysql_fetch
statement你可以使用
mysql_fetch
语句来做到这一点
$mysql_connection2 = mysql_connect($host2, $user2, $pass2, true);
mysql_select_db($db2, $mysql_connection2) or die(mysql_error());
while($row = mysql_fetch_array($select_query))
{
$field1 = $row['field1'];
$field2 = $row['field2'];
$field3 = $row['field3'];
$insert_query = mysql_query("INSERT INTO jungle (desk1, chair1, table1) VALUES ('$field1', '$field2', '$field3')",$mysql_connection2);
if ($insert_query) {
echo "Insert Query is OK <br>";
} else {
echo "Insert Query is not OK <br>";
}
}
This statement loops through your select statement and inserts a row for every returned result using your INSERT
statement此语句遍历您的 select 语句,并使用
INSERT
语句为每个返回的结果插入一行
But really you should also look into mysqli
because mysql
is depreciated.但实际上您也应该查看
mysqli
因为mysql
已贬值。 This is however the basic logic behind what you are doing然而,这是您正在做的事情背后的基本逻辑
why not trying using mysql Trigger if you on the same mysql server, i though that more efective than using php, you can see this question for more information 为什么不尝试使用mysql Trigger,如果你在同一个mysql服务器上,我虽然比使用php更有效,你可以看到这个问题以获取更多信息
http://stackoverflow.com/questions/7128173/mysql-trigger-to-insert-data-into-different-db
AND here 和这里
http://stackoverflow.com/questions/20441783/mysql-create-trigger-insert-content-into-another-database
I get the post is old, but I had to do the same thing and used mysqli as is suggested.我知道帖子很旧,但我必须做同样的事情并按照建议使用 mysqli。 Only thing I would caution is this example uses just a integer field and a short text field.
我唯一要注意的是这个例子只使用了一个整数字段和一个短文本字段。 If you have additional fields you should use prepared statements, however as I know the data here is fixed and contains no special characters so this will be fine for my needs.
如果您有其他字段,您应该使用准备好的语句,但是据我所知,这里的数据是固定的并且不包含特殊字符,所以这可以满足我的需要。
<?php
include "database1.php"; // Using database connection file here conn variable is $connj
include "database2.php"; // Using database connection file here conn variable is $conn
// select data and put values in array from first database
$selectsql = mysqli_query($connj,"SELECT id,name FROM table1
ORDER BY id ASC");
$result = mysqli_query($connj, $selectsql);
while($row = mysqli_fetch_array($selectsql))
{
$id = $row['id'];
$name = $row['name'];
//replace query into second table in second database
$replacesql = mysqli_query($conn, "REPLACE INTO table2 (name,id) VALUES ('$name','$username','$id');");
if($replacesql)
{
echo "Replace Successful<br>";
}
else
{
echo "replace error<br>";
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.