Is it possible to insert multiple data with delay kind of thing/sleep for a few second? for example here, Im going to insert the values (1,2) into my user table. Then after 5 seconds, it will proceed to insert value (3,4) into the same table, wait for 5 seconds and finally insert (5,6) into the table.
INSERT INTO User (col1, col2)
VALUES (1, 2), (3, 4), (5, 6)
Any suggestions are really appreciated!
<?php // da
$array = array(1, 2, 3, 4);
foreach ($array as $data) {
mysqli_query($conn,"INSERT INTO ... SET data='".$data."'");
sleep(1)
}
?>
Something like that? I did not understand very well.
So what I do is create the query by concatenating it like this. But if you want to have multiple inserts you can call that function in a loop and remove this sleep put it in the loop outside the function or you can leave it there if the call is not a thread.
<?php
static function insert_table_db($anArra){
$dbconn = mysqli_connect(DB_HOST, DB_USER,DB_PASSWORD, DB_DATABASE) or die('MySQL connection failed!' . mysqli_connect_error());
mysqli_set_charset($dbconn, "utf8");
$query = "INSERT INTO `table`(`col1`, `col2`, `col3`, `col4`, `col5`) VALUES ";
$i = 1;
foreach ($anArray as $item) {
$query = $query . "(" . $item[0]. "," . $item[1]. "," . $item[2] . "," . $item[3] . ",'" . $item[4] . "')";
if (count($anArray) == $i){
$query = $query . ";";
} else {
$query = $query . ",";
}
$i++;
}
// Run query
if($dbconn->query($query) === true) {
$dbconn->close();
} else {
echo "Database Error: " . $dbconn->error;
$dbconn->close();
}
sleep(5);
}
?>
UPDATE: Sorry if some variables don't make sense I stripped it out of a library I have built and there are some extra things that I didn't delete ;D.
I found the solution. Maybe it's a bit messy but this is what I've come with and solved my problem. Basically I just need to use prepare if I want to reuse the statement.
$stmt = $dbh->prepare("INSERT INTO user (col1, col2) VALUES ('1','2');
$stmt2 = $dbh->prepare("INSERT INTO user (col1, col2) VALUES ('3','4');
$stmt3 = $dbh->prepare("INSERT INTO user (col1, col2) VALUES ('5','6');
{ $stmt->execute();
sleep(5);
$stmt2->execute();
sleep(5);
$stmt3->execute();
}
That's all. Thanks for those who tried to solve this.