添加ON DUPLICATE以循环INSERT INTO mysql

时间:2018-02-09 03:30:12

标签: php mysql

while($table = mysql_fetch_array($res_from)) { 
// echo($table[0] . "<br />"); // optional, only for show the name tables

$table=$table[0];

$tableinfo  = mysql_fetch_array(mysql_query("SHOW CREATE TABLE $table  ",$dblink1)); // retrieve table structure from mysite1.com 

mysql_query(" $tableinfo[1] ",$dblink2); // use found structure to make table on mysite2.com

$res_to = mysql_query("SELECT * FROM $table  ",$dblink1); // select all content from table mysite1.com  
while ($row = mysql_fetch_array($res_to, MYSQL_ASSOC) ) {
    $sql_error = "";
    mysql_query("INSERT INTO $table (".implode(", ",array_keys($row)).") VALUES ('".implode("', '",array_values($row))."')  ",$dblink2); // insert one row into new table on mysite2.com`enter code here`

    $sql_error = mysql_error($dblink2);
}
if (@$sql_error) {
    echo $sql_error."<br />";
}else {
    echo "copy table ".$table." done.<br />";
}
    flush();
}

如何将ON DUPLICATE添加到

mysql_query("INSERT INTO $table (".implode(", ",array_keys($row)).") VALUES ('".implode("', '",array_values($row))."')  ",$dblink2);

(".implode(", ",array_keys($row)).")是列名称数组。

('".implode("', '",array_values($row))."')是列值数组。

0 个答案:

没有答案
相关问题