简体   繁体   English

从一个表中选择多个列并将数据插入到 PHP-MySQL 中不同数据库中的另一个表中

[英]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.

相关问题 从数据库中选择并插入PHP-MySQL中的其他数据库 - Select from a database and insert into a different database in PHP-MySQL 如何在 php-mysql 中使用 pdo 从一台服务器连接(拉取数据)到(插入 - 创建表)另一台服务器,可能是获取? - how to connect ( pull data ) from one server to ( insert into - create table ) another server using pdo in php-mysql , possibly fetch ? PHP-Mysql表从不同的主机加入 - PHP-Mysql table join from different host 根据 PHP-MySQL 中另一个表的字段值在一个表上插入行 - Insert rows on one table based on the values of a field of another table in PHP-MySQL 如何使用SESSION数据来选择1个数据库条目,然后使用PHP和MySQL的表单中的数据插入到另一个表中? - How do I use SESSION data to SELECT 1 database entry and then INSERT into another table with data from a form as well with PHP and MySQL? 在php-mysql中更新表 - Updating table in php-mysql 多次插入(少号)与在单列中存储php-Mysql - Multiple(Less no.) Insert into 3 column table vs Storing in Single Column Php-Mysql 从db表1中选择数据并将其插入到php中的另一个db表2中 - Select data from db table 1 and insert it into another db table 2 in php 比较基于日期时间的PHP-MySQL中的两个查询,并将所选数据插入表中 - Compare two queries in PHP-MySQL based on datetime and insert selected data into a table PHP MySQL将数据从一个表插入到另一个表 - PHP MySQL insert data from one table to another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM