4

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_setopt($ch, CURLOPT_URL,$path);
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $retValue = curl_exec($ch);                      
    curl_close($ch);
    return $retValue;
}
$sXML = download_page('http://remotepage.php&function=getItems&count=100&page=1');
$oXML = new SimpleXMLElement($sXML);
foreach($oXML->results->item->item as $oEntry){
    $insert_query = mysql_query("INSERT INTO tbl_item (first_name, last_name, date_added) VALUES ('" . $oEntry->firstname . "', '" . $oEntry->lastname . "', '" . date("Y-m-d H:i:s") . "')");
}

The script works however is extremely slow to insert as I imagine its because its writing each individual record. The count variable is how many records returned for each page and the page variable is a simple page counter.

I am wondering if there is a way to do a bulk insert statement to insert all 100 records at once.

Thanks in advance.

4
  • 1
    en.wikipedia.org/wiki/SQL_injection Commented May 16, 2012 at 9:35
  • 1
    Are you sure its the INSERT that is slow and not the cURL request? Try echoing some timing out to the page to make sure you know where its slowing down. Commented May 16, 2012 at 9:37
  • Please go through this php.net/manual/en/mysqli.multi-query.php Commented May 16, 2012 at 9:52
  • You can add multiple records in one insertion with the code that @magnetik, @chauhan or @Repox have provided. Or you can use prepared statements, that will help against SQL Injections (or use mysql_real_escape_string) and precompile the query so it will be faster if you execute multiple INSERT statements. Commented May 16, 2012 at 10:33

4 Answers 4

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Remove the last comma from $query before inserting it to the database: $query = substr($query, 0, -1);
0

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');

Comments

0

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

Comments

0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.