October 26, 2024
Chicago 12, Melborne City, USA
PHP

Php, wait 5 seconds before executing an action


I have a php function with a foreach loop that inserts data into the database, I have a large data size exceeding 1.5 million lines and this causes the process to stop due to server limits.

I will try to simplify the process:

I want to run the process for the first 10 seconds, then stop it for 5 seconds, complete the process after 5 seconds, run again for 10 seconds, stop for another 5 seconds, and so on until all data is entered.

I trying to add sleep(10) as mentioned in this question answers but it’s not working: Php, wait 5 seconds before executing an action

Example of my function:

function importDatabaseTables($dbHost, $dbUname, $dbPass, $dbName, $filePath){
// Connect & select the database
$db = new mysqli($dbHost, $dbUname, $dbPass, $dbName); 

// Temporary variable, used to store current query
$templine="";

// Read in entire file
$lines = file($filePath);

$error="";

// Loop through each line
foreach ($lines as $line){
    // Skip it if it's a comment
    if(substr($line, 0, 2) == '--' || $line == ''){
        continue;
    }
    
    // Add this line to the current segment
    $templine .= $line;
    
    // If it has a semicolon at the end, it's the end of the query
    if (substr(trim($line), -1, 1) == ';'){
        // Perform the query
        if(!$db->query($templine)){
            $error .= 'Error importing query "<b>' . $templine . '</b>": ' . $db->error . '<br /><br />';
        }
        
        // Reset temp variable to empty
        $templine="";
    }
    
    
}
return !empty($error)?$error:true;

}



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video