0

I have to insert a few rows at once and every row gets the same value. Is there a way to shorten that query?

$stmt = $db->prepare('
    INSERT INTO `tablename`
    (`value`) VALUES (?), (?), (?), (?), (?)
');
$stmt->execute(array($value, $value, $value, $value, $value));
8
  • Have you tried executing this query? Commented Sep 11, 2013 at 15:39
  • Do you mean row (then see my answer below) or field? If the latter probably no. Commented Sep 11, 2013 at 15:39
  • That's what a fully qualified INSERT query looks like. Are you arguing that the SQL standard should be changed? Commented Sep 11, 2013 at 15:42
  • @Paddyd Yes, the query works fine. Commented Sep 11, 2013 at 15:44
  • @tadman No, no, that's not my intention. The question is more like: "Can I re-use a value multiple times in a prepared statement". Hope this makes it clearer. Commented Sep 11, 2013 at 15:46

4 Answers 4

2
$count = 5;
$value = 'HELLO';
$stmt = $db->prepare('
    INSERT INTO `tablename`
    (`value`) VALUES ' . implode(', ', array_fill(0, $count, '(?)')) );
$stmt->execute(array_fill(0, $count, $value));

Though I'm not sure I see much value in filling n rows of a table with identical values

Sign up to request clarification or add additional context in comments.

3 Comments

Shorter and more flexible at the expense of maintainability. But shorter.
+1 I think I'll go with this, as it's more readable and even better maintainable then having lots of (?). :) Thanks.
@MarkBaker I know this is not the most appropriate way to ask and so on, but could you please take a look at my question related to PHPExcel? stackoverflow.com/questions/18742595/… I would be really grateful if you could help, though you do not have to answer.
1

If you can put the values in another table, you can repeatedly INSERT these:

CREATE TEMPORARY TABLE _values (v VARCHAR(255));
INSERT INTO _values ("x","x","x");

/* Repeat as necessary */
INSERT INTO tablename (`value`) SELECT v FROM _values;

1 Comment

+1 This looks very interesting and is probbaly very fast when inserting lots of lots of rows? I'll keep that in mind, for later. Sorry that I can only accept one answer. Still many thanks for sharing this and for your effort.
0

Seeing as you're using prepared statements, why not call execute() on a loop?:

$rowsToInsert = 5;

$stmt = $db->prepare('INSERT INTO `tablename` (`value`) VALUES (?)');
for($i = 0; $i < $rowsToInsert; $i++) {
    $stmt->execute(array($value));
}

It's certainly much easier to control programmatically than a single query multi-row insert.

3 Comments

This is also a lot slower since each INSERT is a transaction and will have MVCC and indexing overhead.
If performance is an issue, PDO's beginTransaction() and commit() could be used :/
There's still overhead for each statement. A mass-insert will win nearly every time, especially if there's a large number of rows being inserted.
0

I am not sure if below works.
But at least it gives you the syntax for several insertions at once.

$stmt = $db->prepare('
INSERT INTO example
  (example_id, name, value, other_value)
VALUES
  (?, ?, ?, ?),
  (?, ?, ?, ?),
  (?, ?, ?, ?),
  (?, ?, ?, ?);
');
$stmt->execute(
    array(
        $value, $value, $value, $value, $value, 
        $value, $value, $value, $value, $value, 
        $value, $value, $value, $value, $value, 
        $value, $value, $value, $value, $value
));

3 Comments

This answer is not exactly answering anything. This is longer.
This isn't really shorter, isn't it? ;) And I only need to set one value per row. But it gets very long when inserting 10 or even more rows.
You can create the array with a function. Then you can create the SQL with a function too. That would make your code short and readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.