简体   繁体   中英

$wpdb->insert : problems adding multiple records ( nearly 1000)

I need to add nearly 1000 records from my plugin. While trying sometimes I have not all records are added. Also the number of records varies. Sometimes it is 300 , sometimes it is 400 , sometimes it is 200 etc.

Initial query was:

foreach ($data as $value) {
  $success=$wpdb->insert( 
    $table, 
    array( 
      'url' => $value, 
      'status' => 'n',
    ), 
    array( 
      '%s', 
      '%s' 
    ) ); 
}

I though some problems maybe with $value . Then to test I tried with the following:

for($i=1;$i <= 1000;$i++){ 
  $success=$wpdb->insert( 
    $table, 
    array( 
      'url' => $i, 
      'status' => 'n',
    ), 
    array( 
      '%s', 
      '%s' 
    ) ); 
 }

It is also behaving the same. Sometimes not 1000 records get added. And the number of insertions vary with each execution .

I'm using AJax to process it. The code is given below. Is the cause lied in Ajax.


jQuery(document).ready(function($) {

  jQuery('#sbswp').submit( function (event){

    var data = {
      'action': 'sbi2wp_action',
      url : $('input[name=url]').val(),
    };

    $.post(ajaxurl, data, function(response) {
      $('#result').html(response);
    });
    return false;
  });

});

How much time does it run before it fails? It could be that you're hitting PHP's limits of execution time or memory. You can add these two lines at the beginning of PHP code to try it:

// allows php script to run for 3000 seconds before it's terminated
ini_set('max_execution_time', 3000);

// allows php to use up to 512 megabytes of RAM
ini_set('memory_limit', '512M');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM