Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php多个mysql插入_Php_Mysql - Fatal编程技术网

php多个mysql插入

php多个mysql插入,php,mysql,Php,Mysql,我想从db SELECT from pages的一个表中读取数据,并根据插入内容将它们复制到多个表中。当我复制到一个或另一个表时,我的脚本工作正常,但当我像下面那样运行它时,它只会将数据复制到第一个表中。你们能帮帮我吗 <?php // Pripojenie na databazu s overenim pripojenia $con = mysql_connect('localhost:8501' , 'root', ''); if(!$con) { die(

我想从db SELECT from pages的一个表中读取数据,并根据插入内容将它们复制到多个表中。当我复制到一个或另一个表时,我的脚本工作正常,但当我像下面那样运行它时,它只会将数据复制到第一个表中。你们能帮帮我吗

      <?php

 // Pripojenie na databazu s overenim pripojenia
 $con = mysql_connect('localhost:8501' , 'root', '');

if(!$con) {
    die("Nepripojene ".mysql_error());
}  

 // Vyber databazy ikarus2 s overenim vyberu    
 $db = mysql_select_db('ikarus2',$con);
if(!$db) {
    die('Ziadny pristup k db ikarus2!'.mysql_error());
} 

 // MySQL SELECT from pages
 $result = mysql_query('SELECT uid, t3ver_oid, t3ver_id, t3ver_wsid, t3ver_label,      t3ver_state, t3ver_stage,
                t3ver_count, t3ver_tstamp, t3_origuid, tstamp, crdate, cruser_id,
                title, hidden, starttime, endtime, deleted, subtitle, nav_title, media, keywords, description, abstract, author, author_email,
                tx_impexp_origuid, tx_realurl_pathsegment, doktype, url, urltype, shortcut, shortcut_mode, t3ver_move_id, sorting,
                layout, fe_group FROM pages',$con);


 // MySQL INSERT into pages_language_overlay
 while ($row = mysql_fetch_array($result)){
$insertoverlay = "INSERT INTO pages_language_overlay( pid, t3ver_oid, t3ver_id,  t3ver_wsid, t3ver_label, t3ver_state, t3ver_stage,
                        t3ver_count, t3ver_tstamp,  t3_origuid, tstamp, crdate, cruser_id, sys_language_uid, title, hidden, starttime,   endtime, 
                        deleted, subtitle, nav_title,    media, keywords, description, abstract, author, author_email, tx_impexp_origuid, tx_realurl_pathsegment,
                        doktype, url, urltype, shortcut, shortcut_mode) 
            VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','$row[8]',
                        '$row[9]','$row[10]','$row[11]','$row[12]','5', '$row[13]','$row[14]','$row[15]','$row[16]','$row[17]','$row[18]',
                        '$row[19]','$row[20]','$row[21]','$row[22]','$row[23]','$row[24]','$row[25]','$row[26]','$row[27]','$row[28]',
                        '$row[29]','$row[30]','$row[31]','$row[32]')";
mysql_query($insertoverlay,$con);

 }

 // MySQL INSERT into tt_content
 while ($row = mysql_fetch_array($result)){
$insertcontent = "INSERT INTO tt_content( pid, t3ver_oid, t3ver_id, t3ver_wsid,    t3ver_label, t3ver_state, t3ver_stage, t3ver_count,
 t3ver_tstamp, t3ver_move_id, t3_origuid, tstamp, crdate, cruser_id, hidden, sorting,  media, layout, 
 deleted, starttime, endtime, fe_group, sys_language_uid, tx_impexp_origuid) 
            VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row [4]','$row[5]','$row[6]','$row[7]','$row[8]',
                        '$row[33]','$row[9]','$row[10]','$row[11]','$row[12]','$row[14]','$row[34]','$row[20]','$row[35]','$row[17]',
                        '$row[15]','$row[16]','$row     [36]','5','$row[26]')";
mysql_query($insertcontent,$con);   

   }

 ?>
我认为多重while循环会导致问题。试一试

    while ($row = mysql_fetch_array($result)){

        $insertoverlay = "INSERT INTO pages_language_overlay( pid, t3ver_oid, t3ver_id,  t3ver_wsid, t3ver_label, t3ver_state, t3ver_stage,
                                t3ver_count, t3ver_tstamp,  t3_origuid, tstamp, crdate, cruser_id, sys_language_uid, title, hidden, starttime,   endtime, 
                                deleted, subtitle, nav_title,    media, keywords, description, abstract, author, author_email, tx_impexp_origuid, tx_realurl_pathsegment,
                                doktype, url, urltype, shortcut, shortcut_mode) 
                    VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','$row[8]',
                                '$row[9]','$row[10]','$row[11]','$row[12]','5', '$row[13]','$row[14]','$row[15]','$row[16]','$row[17]','$row[18]',
                                '$row[19]','$row[20]','$row[21]','$row[22]','$row[23]','$row[24]','$row[25]','$row[26]','$row[27]','$row[28]',
                                '$row[29]','$row[30]','$row[31]','$row[32]')";
        mysql_query($insertoverlay,$con);

$insertcontent = "INSERT INTO tt_content( pid, t3ver_oid, t3ver_id, t3ver_wsid,    t3ver_label, t3ver_state, t3ver_stage, t3ver_count,
 t3ver_tstamp, t3ver_move_id, t3_origuid, tstamp, crdate, cruser_id, hidden, sorting,  media, layout, 
 deleted, starttime, endtime, fe_group, sys_language_uid, tx_impexp_origuid) 
            VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row [4]','$row[5]','$row[6]','$row[7]','$row[8]',
                        '$row[33]','$row[9]','$row[10]','$row[11]','$row[12]','$row[14]','$row[34]','$row[20]','$row[35]','$row[17]',
                        '$row[15]','$row[16]','$row     [36]','5','$row[26]')";
mysql_query($insertcontent,$con);   

         }
1.-不要使用mysql_*函数,如果已弃用,请使用PDO或mysqli_*函数

2.-我认为您不太了解mysql\u fetch\u数组,此函数返回下一行数据并将指针移动到下一条记录,因此,在第二个while循环中,mysql\u fetch\u array将返回false

因此,您需要在同一while/loop中执行两条insert语句:

// MySQL INSERT into pages_language_overlay
 while ($row = mysql_fetch_array($result)){
$insertoverlay = "INSERT INTO pages_language_overlay( pid, t3ver_oid, t3ver_id,  t3ver_wsid, t3ver_label, t3ver_state, t3ver_stage,
                        t3ver_count, t3ver_tstamp,  t3_origuid, tstamp, crdate, cruser_id, sys_language_uid, title, hidden, starttime,   endtime, 
                        deleted, subtitle, nav_title,    media, keywords, description, abstract, author, author_email, tx_impexp_origuid, tx_realurl_pathsegment,
                        doktype, url, urltype, shortcut, shortcut_mode) 
            VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','$row[8]',
                        '$row[9]','$row[10]','$row[11]','$row[12]','5', '$row[13]','$row[14]','$row[15]','$row[16]','$row[17]','$row[18]',
                        '$row[19]','$row[20]','$row[21]','$row[22]','$row[23]','$row[24]','$row[25]','$row[26]','$row[27]','$row[28]',
                        '$row[29]','$row[30]','$row[31]','$row[32]')";
mysql_query($insertoverlay,$con);

$insertcontent = "INSERT INTO tt_content( pid, t3ver_oid, t3ver_id, t3ver_wsid,    t3ver_label, t3ver_state, t3ver_stage, t3ver_count,
 t3ver_tstamp, t3ver_move_id, t3_origuid, tstamp, crdate, cruser_id, hidden, sorting,  media, layout, 
 deleted, starttime, endtime, fe_group, sys_language_uid, tx_impexp_origuid) 
            VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row [4]','$row[5]','$row[6]','$row[7]','$row[8]',
                        '$row[33]','$row[9]','$row[10]','$row[11]','$row[12]','$row[14]','$row[34]','$row[20]','$row[35]','$row[17]',
                        '$row[15]','$row[16]','$row     [36]','5','$row[26]')";
mysql_query($insertcontent,$con);   

   }

在执行第二个while循环之前,请尝试使用:

记录集$result中的指针需要重置为零,否则它仍将指示它位于末尾


首先将所有记录存储在一个数组中,然后执行foreach将数据插入数据库

程序结构不好。当控件到达第二个循环时,结果集已被完全消耗。相反,将它们放在同一个循环中,以解决您面临的直接问题:

 // MySQL INSERT into pages_language_overlay
 while ($row = mysql_fetch_array($result)){
$insertoverlay = "INSERT INTO pages_language_overlay( pid, t3ver_oid, t3ver_id,  t3ver_wsid, t3ver_label, t3ver_state, t3ver_stage,
                    t3ver_count, t3ver_tstamp,  t3_origuid, tstamp, crdate, cruser_id, sys_language_uid, title, hidden, starttime,   endtime, 
                    deleted, subtitle, nav_title,    media, keywords, description, abstract, author, author_email, tx_impexp_origuid, tx_realurl_pathsegment,
                    doktype, url, urltype, shortcut, shortcut_mode) 
        VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','$row[8]',
                    '$row[9]','$row[10]','$row[11]','$row[12]','5', '$row[13]','$row[14]','$row[15]','$row[16]','$row[17]','$row[18]',
                    '$row[19]','$row[20]','$row[21]','$row[22]','$row[23]','$row[24]','$row[25]','$row[26]','$row[27]','$row[28]',
                    '$row[29]','$row[30]','$row[31]','$row[32]')";
mysql_query($insertoverlay,$con);

$insertcontent = "INSERT INTO tt_content( pid, t3ver_oid, t3ver_id, t3ver_wsid,    t3ver_label, t3ver_state, t3ver_stage, t3ver_count,
 t3ver_tstamp, t3ver_move_id, t3_origuid, tstamp, crdate, cruser_id, hidden, sorting,  media, layout, 
 deleted, starttime, endtime, fe_group, sys_language_uid, tx_impexp_origuid) 
        VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row [4]','$row[5]','$row[6]','$row[7]','$row[8]',
                    '$row[33]','$row[9]','$row[10]','$row[11]','$row[12]','$row[14]','$row[34]','$row[20]','$row[35]','$row[17]',
                    '$row[15]','$row[16]','$row     [36]','5','$row[26]')";
    mysql_query($insertcontent,$con);   

   }

 ?>
不过,您可能也希望在普通SQL中使用insert命令,该命令可用于选定的结果集。 :

这样做可能会更快:数据不需要传输到运行PHP代码的服务器,然后返回到DB——它完全由DB处理

但是,也请注意,从PHP5.5开始,mysql_*函数就已经被弃用,因此您应该认真地转向其他解决方案。我建议使用PDO。
您可能想要的也可以在普通SQL中使用,您可以使用insert命令来处理选定的结果集。mysql_*函数不再维护,不应在任何新的代码库中使用。它正逐渐被淘汰,取而代之的是更新的API。相反,您应该与或一起使用。
 // MySQL INSERT into pages_language_overlay
 while ($row = mysql_fetch_array($result)){
$insertoverlay = "INSERT INTO pages_language_overlay( pid, t3ver_oid, t3ver_id,  t3ver_wsid, t3ver_label, t3ver_state, t3ver_stage,
                    t3ver_count, t3ver_tstamp,  t3_origuid, tstamp, crdate, cruser_id, sys_language_uid, title, hidden, starttime,   endtime, 
                    deleted, subtitle, nav_title,    media, keywords, description, abstract, author, author_email, tx_impexp_origuid, tx_realurl_pathsegment,
                    doktype, url, urltype, shortcut, shortcut_mode) 
        VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]','$row[7]','$row[8]',
                    '$row[9]','$row[10]','$row[11]','$row[12]','5', '$row[13]','$row[14]','$row[15]','$row[16]','$row[17]','$row[18]',
                    '$row[19]','$row[20]','$row[21]','$row[22]','$row[23]','$row[24]','$row[25]','$row[26]','$row[27]','$row[28]',
                    '$row[29]','$row[30]','$row[31]','$row[32]')";
mysql_query($insertoverlay,$con);

$insertcontent = "INSERT INTO tt_content( pid, t3ver_oid, t3ver_id, t3ver_wsid,    t3ver_label, t3ver_state, t3ver_stage, t3ver_count,
 t3ver_tstamp, t3ver_move_id, t3_origuid, tstamp, crdate, cruser_id, hidden, sorting,  media, layout, 
 deleted, starttime, endtime, fe_group, sys_language_uid, tx_impexp_origuid) 
        VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row [4]','$row[5]','$row[6]','$row[7]','$row[8]',
                    '$row[33]','$row[9]','$row[10]','$row[11]','$row[12]','$row[14]','$row[34]','$row[20]','$row[35]','$row[17]',
                    '$row[15]','$row[16]','$row     [36]','5','$row[26]')";
    mysql_query($insertcontent,$con);   

   }

 ?>
INSERT INTO destTable (col1, col2, col3) 
    SELECT srcCol1, srcCol2, srcCol3 FROM srcTable;