Reputation: 169
I want to insert million rows in database table and I'm trying to insert by 1000 rows each time by pushing values in array.
$pdo = new PDO ('mysql:host=localhost;port=3306;dbname=dbname;', 'root', '');
$batch_size = 1000;
$sql = "INSERT INTO `table` (`sector`,`keyword`,`location`,`position`) VALUES ";
// Add 1,000 value lists
$sql .= str_repeat("(?,?,?,?),", $batch_size-1);
$sql .= "(?,?,?,?),";
$stmt = $pdo->prepare($sql);
for ($i = 0; $i < (1000000/$batch_size); $i++) {
$vals = array();
for ($j = 0; $j < $batch_size; $j++) {
array_push("5","5","5","5"); // <-- error
}
$stmt->execute($vals);
}
I get error: Only variables can be passed by reference in line: (array_push...) Can you suggest solution? Thanks
Upvotes: 0
Views: 249
Reputation: 6679
You're missing the array you're trying to push in.
The docs tell you:
Parameters
array
- The input array.
...
- The values to push onto the end of the array.
You insert 4 parameters in this part: array_push("5","5","5","5");
You need to add the array you want to array_push. Like this:
array_push($vals, "5", "5","5","5");
Upvotes: 2