PHP bulk insert foreach

前端 未结 4 1188
一整个雨季
一整个雨季 2021-01-03 05:54

I have a curl script which reads the data from a remote source. Below is the current code:

function download_page($path){
    $ch = curl_init();
    curl_set         


        
相关标签:
4条回答
  • 2021-01-03 06:29

    to bulk insert operation you can append you script in a variable and you can run this as whole but for this you have to use mysqli_multi_query. Check this link http://www.php.net/manual/en/mysqli.multi-query.php

    0 讨论(0)
  • 2021-01-03 06:36

    You can insert all records at once as below: Like when we export data from mysql table.

    INSERT INTO tablename (id, field2, field3, field3) VALUES ('', 5454, '454', '545'), ('', 'erwe', 'rewrew', 'werew'), ('', 'ewrew', 'rwerwe', 'werwer'), ('', 'jkj', 'ere', 'uju') , ('', '343', '3434', 'dfdf');
    
    0 讨论(0)
  • 2021-01-03 06:37

    You can do this within one statement by doing something like this :

    $sXML = download_page('http://remotepage.php&function=getItems&count=100&page=1');
    $oXML = new SimpleXMLElement($sXML);
    $query = "INSERT INTO tbl_item (first_name, last_name, date_added) VALUES";
    foreach($oXML->results->item->item as $oEntry){
        $query .=  "('" . $oEntry->firstname . "', '" . $oEntry->lastname . "', '" . date("Y-m-d H:i:s") . "'),";
    }
    mysql_query($query);
    
    0 讨论(0)
  • 2021-01-03 06:39

    Maybe something like this?

    foreach($oXML->results->item->item as $oEntry){
      $inserts[] = "(". implode(', ', array ($oEntry->firstname, $oEntry->lastname, date("Y-m-d H:i:s") ) ) .")";
    }
    $insert_query = mysql_query("INSERT INTO tbl_item (first_name, last_name, date_added) VALUES ".implode(', ', $inserts));
    
    0 讨论(0)
提交回复
热议问题