P粉5305192342023-08-25 15:20:13
Try again, I don't understand why your original code doesn't work after a slight modification:
$query = "INSERT INTO table (link) VALUES (?)"; $stmt = $mysqli->prepare($query); $stmt->bind_param("s", $one); foreach ($array as $one) { $stmt->execute(); } $stmt->close();
P粉3409802432023-08-25 00:31:22
By putting your inserts into a transaction you should be able to speed things up a lot. You can also move prepare and bind statements outside the loop.
$array = array("array", "with", "about", "2000", "values"); $query = "INSERT INTO table (link) VALUES (?)"; $stmt = $mysqli->prepare($query); $stmt ->bind_param("s", $one); $mysqli->query("START TRANSACTION"); foreach ($array as $one) { $stmt->execute(); } $stmt->close(); $mysqli->query("COMMIT");
I tested this code on my web server for 10,000 iterations.
No transaction: 226 seconds.
Transaction time: 2 seconds.
Or be two orders of magnitude faster, at least for this test.