Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
How can I do this:
for ($i=0; $i<$number; $i++) { mysql_query("INSERT INTO blah (foo, bar) VALUES (".$array[$i].", 1)"); }
With just one INSERT?
INSERT
Is it possible?
PS: I know mysql_query is deprecated.
mysql_query
INSERT INTO blah(foo, bar) VALUES (...), (...), (...), (...), ...
$number
You can pass multiple VALUES in INSERT statement like:
VALUES
INSERT INTO blah(foo, bar) VALUES (...), (...), (...), (...),...
Add a comment
You can do:
$stmt = ""; for ($i = 0; $i < $number; $i++) { $stmt .= "INSERT INTO blah (foo, bar) VALUES (" . $array[$i] . ", 1);"; } //deprecated: mysql_multi_query($stmt); mysqli_multi_query($stmt);
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
INSERT INTO blah(foo, bar) VALUES (...), (...), (...), (...), ...$numberis not a known constant, it is impossible to do this without looping at some stage.