Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL

Sami

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. 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.

enter image description here

Chitowns24

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_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

But really you should also look into mysqli because mysql is depreciated. This is however the basic logic behind what you are doing

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql insert into with multiple id from another table

From Dev

How to select data from a table and insert into another table?

From Dev

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

From Dev

MySQL Insert INTO using SELECT from another table with additional data

From Dev

php mysql select set of columns from table one based on values of colons in another table

From Dev

Insert multiple rows into a MySQL database from a table

From Dev

Insert multiple rows from select into another table

From Dev

mysql: select rows from another table as columns

From Dev

MySQL - Insert table data from another table

From Dev

Multiple insert in MySQL table from PHP table

From Dev

Select from table where columns names equal data in a different table

From Dev

How to select data from a table and insert into another table?

From Dev

MySQL/PHP check if data is in database table, if not insert it

From Dev

looping mysql query to get multiple rows of data from a table and insert into another table

From Dev

insert multiple rows mysql from another table

From Dev

PHP: Show data from mysql database in table

From Dev

How to insert data from 3 different input fields together in one column of table in mysql database in codeigniter

From Dev

Select table from Sql server and insert data to Mysql table

From Dev

How to insert and retrive data from one table to another table in mysql database

From Dev

MySQL - Insert table data from another table

From Dev

Multiple insert in MySQL table from PHP table

From Dev

MySQL insert into column data from another table

From Dev

Retrieve data from mysql database and insert it in a table using php

From Dev

How to insert multiple columns with different select conditions into another table using SQL

From Dev

Insert Data From One Table To Another - MySQL

From Dev

mysql insert into table select from another database

From Dev

Insert specific rows from one table in database into another with different columns

From Dev

MySql: insert into table SELECT from another table where first_table ID =another_table.id

From Dev

PHP MYSQL - insert a new record but requires column from select statement in another table - fails in php

Related Related

  1. 1

    Mysql insert into with multiple id from another table

  2. 2

    How to select data from a table and insert into another table?

  3. 3

    How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

  4. 4

    MySQL Insert INTO using SELECT from another table with additional data

  5. 5

    php mysql select set of columns from table one based on values of colons in another table

  6. 6

    Insert multiple rows into a MySQL database from a table

  7. 7

    Insert multiple rows from select into another table

  8. 8

    mysql: select rows from another table as columns

  9. 9

    MySQL - Insert table data from another table

  10. 10

    Multiple insert in MySQL table from PHP table

  11. 11

    Select from table where columns names equal data in a different table

  12. 12

    How to select data from a table and insert into another table?

  13. 13

    MySQL/PHP check if data is in database table, if not insert it

  14. 14

    looping mysql query to get multiple rows of data from a table and insert into another table

  15. 15

    insert multiple rows mysql from another table

  16. 16

    PHP: Show data from mysql database in table

  17. 17

    How to insert data from 3 different input fields together in one column of table in mysql database in codeigniter

  18. 18

    Select table from Sql server and insert data to Mysql table

  19. 19

    How to insert and retrive data from one table to another table in mysql database

  20. 20

    MySQL - Insert table data from another table

  21. 21

    Multiple insert in MySQL table from PHP table

  22. 22

    MySQL insert into column data from another table

  23. 23

    Retrieve data from mysql database and insert it in a table using php

  24. 24

    How to insert multiple columns with different select conditions into another table using SQL

  25. 25

    Insert Data From One Table To Another - MySQL

  26. 26

    mysql insert into table select from another database

  27. 27

    Insert specific rows from one table in database into another with different columns

  28. 28

    MySql: insert into table SELECT from another table where first_table ID =another_table.id

  29. 29

    PHP MYSQL - insert a new record but requires column from select statement in another table - fails in php

HotTag

Archive