Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to INSERT many values in mysqli?

Tags:

php

mysql

mysqli

I'm looking for a SQL-injection-secure technique to insert a lot of rows (ca. 2000) at once with PHP and MySQLi.
I have an array with all the values that have to be include. Currently I'm doing that:

<?php
$array = array("array", "with", "about", "2000", "values");

foreach ($array as $one) 
{
    $query = "INSERT INTO table (link) VALUES ( ?)";
    $stmt = $mysqli->prepare($query);
    $stmt ->bind_param("s", $one);
    $stmt->execute();
    $stmt->close();
}
?>

I tried call_user_func_array(), but it caused a stack overflow.

What is a faster method to do this (like inserting them all at once?), but still secure against SQL injections (like a prepared statement) and stack overflows?

like image 467
Roman Holzner Avatar asked Mar 01 '13 01:03

Roman Holzner


People also ask

How can I add multiple values in one column in MySQL?

First, specify the name of table that you want to insert after the INSERT INTO keywords. Second, specify a comma-separated column list inside parentheses after the table name. Third, specify a comma-separated list of row data in the VALUES clause. Each element of the list represents a row.

How can I insert multiple rows in SQL using single query in PHP?

Inserting Multiple Rows into a Table. One can also insert multiple rows into a table with a single insert query at once. To do this, include multiple lists of column values within the INSERT INTO statement, where column values for each row must be enclosed within parentheses and separated by a comma.

How do I run multiple inserts in SQL?

The normal SQL INSERT query inputs the data values in a single row. In case when we want to insert data in multiple rows at once, this query fails. Thus, in order to save the execution time, we need to use the SQL INSERT query in such a manner that it injects data into multiple rows at once.

How can I store multiple values in a single column in MySQL using PHP?

Insert Mutliple values in a single Field: For inserting the flat no's in a single field, use implode and the values separated by comma. For retrieve the values from database use, explode to separate out all the values.. For Retrieve the values using $i=explode(",",$r);


3 Answers

You should be able to greatly increase the speed by putting your inserts inside a transaction. You can also move your prepare and bind statements outside of your 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 with 10,000 iterations on my web server.

Without transaction: 226 seconds. With transaction: 2 seconds. Or a two order of magnitude speed increase, at least for that test.

like image 151
Dan Metheus Avatar answered Oct 23 '22 06:10

Dan Metheus


Trying this again, I don't see why your original code won't work with minor modifications:

$query = "INSERT INTO table (link) VALUES (?)";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("s", $one);

foreach ($array as $one) {
    $stmt->execute();
}
$stmt->close();
like image 33
Explosion Pills Avatar answered Oct 23 '22 06:10

Explosion Pills


Yes, you can build a single big query manually, with something like:

$query = "";
foreach ($array as $curvalue) {
  if ($query)
    $query .= ",";
  $query .= "('" . $mysqli->real_escape_string($curvalue) . "')";
}
if ($query) {
  $query = "INSERT INTO table (link) VALUES " . $query;
  $mysqli->query($query);
}
like image 31
Mark Ormston Avatar answered Oct 23 '22 07:10

Mark Ormston