Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL -
i have select data table in database , insert them table in different database. below code returns $select_query
ok, $insert_query
not ok. please correct code , let me know 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 tree date_entered > '2014-01-01 16:22:00'", $mysql_connection1); $number = mysql_num_rows($select_query); if ($select_query) { echo "select query ok <br>"; echo $number ."<br>"; } else { echo "select query 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 jungle (desk1, chair1, table1) values ('$field1', '$field2', '$field3')", $mysql_connection2); if ($insert_query) { echo "insert query ok <br>"; } else { echo "insert query not ok <br>"; } } mysql_close($mysql_connection1); mysql_close($mysql_connection2);
below can see schematic image of tables.
you need establish values?
"insert jungle (desk1, chair1, table1) values (value1, value2, value3)"
you should collect data first select , can set pertinent values in insert statement
you can using 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 jungle (desk1, chair1, table1) values ('$field1', '$field2', '$field3')",$mysql_connection2); if ($insert_query) { echo "insert query ok <br>"; } else { echo "insert query not ok <br>"; } }
this statement loops through select statement , inserts row every returned result using insert
statement
but should mysqli
because mysql
depreciated. basic logic behind doing
Comments
Post a Comment