Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8730297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:59:33+00:00 2026-06-13T08:59:33+00:00

Skip to My Code at the bottom of the question to get straight to

  • 0

Skip to “My Code” at the bottom of the question to get straight to the point.


$con = mysql_connect('localhost', 'mysql_user', 'mysql_password');


General Examples:

Multiple query strings can be executed within a single mysql_query, like this…

mysql_query("
    INSERT INTO table_one (a,b,c) VALUES (1,2,3);
    INSERT INTO table_two (x,y,z) VALUES (7,8,'a text value');
    ", $con);

Multiple rows can be added to each query string, like this…

mysql_query("
    INSERT INTO table_one (a,b,c) VALUES (1,2,3), (2,3,1), (3,1,2);
    INSERT INTO table_two (x,y,z) VALUES (7,8,9), (8,9,7), (9,7,8);
    ", $con);

With an array of values, like this…

$array(
  [0] => array(1,2,3),
  [1] => array(2,'text',1),
  [2] => array(3,1,2)
);

…a query string can be dynamically created using a loop, like this…

$a = "INSERT INTO table_one (a,b,c) VALUES ";
foreach($array as $val){
  $a .= "(". implode(",",$val) ."),";
}
$a = trim($a, ",") . ";";

The Question:

How to write a single MySQL query that executes multiple INSERT query strings, each with multiple rows, so that each row inserted by the second query string can reference the corresponding row inserted by the first query string. Thus $var in this snippet would correspond to the auto_increment row ID of the first INSERT.

mysql_query("
    INSERT INTO table_one (a,b,c) VALUES (1,2,3), (2,3,1), (3,1,2);
    INSERT INTO table_two (x,y,z) VALUES ($var,8,9), ($var,9,7), ($var,7,8);
    ", $con);

My Code:

This is a simplified version of my code…

$q1 = "INSERT INTO `table_one` (t1c1,t1c2) VALUES ";
$q2 = "INSERT INTO `table_two` (t2c1,t2c2,t2c3) VALUES ";

foreach($array as $item){
  $q1 .= "('". $item ."','blah'),";
  $q2 .= "('". $row_ID_from_first_insert ."','something','whatever'),";
}

$q1 = trim($q1,",") .";";
$q2 = trim($q2,",") .";";
$q = $q1.$q2;

mysql_query($q, $this->connect);

What is a safe way to generate $row_ID_from_first_insert?

From my understanding using LAST_INSERT_ID() in the query string will not work because when multiple rows are inserted with a single query string it only returns the first inserted row ID. Is it safe to use LAST_INSERT_ID()+1 where 1 is replaced with a incremental variable in the foreach loop? What if multiple submits occur at the same time from different users?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:59:34+00:00Added an answer on June 13, 2026 at 8:59 am

    Generally you’ll have to fetch back out the id values of the inserted rows. This is unless you can create another key that is used in place of that which can be procedurally generated in advance.

    While INSERT statements with multiple rows is significantly faster, the price you pay is a lack of precision. If your id values are issued sequentially, which is the default behavior, then you can be reasonably assured that the LAST_INSERT_ID() represents the ID of the first row inserted, so you could perhaps calculate the ID of the rows using the method you propose, though starting at index 0.

    Remember that multiple submissions do not affect the LAST_INSERT_ID() result because this value is per-connection. It is only changed if you perform a subsequent query on the same database handle. Operations in other requests will not affect it.

    Databases that are clustered or multi-master may issue non-sequential identifiers in which case this won’t work. This is also unreliable in an INSERT IGNORE situation where the number of rows actually inserted could be less than the number of rows provided.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: skip to bottom for real question Hey guys, right now I am in
Skip to bottom for question, but first, a little context. So I have been
Skip to the bottom for the question; this is just some extra info I
I can't get the new code I've written to skip the first row (header)
The Background (skip to the bottom if you want the question) Recently I upgraded
There is a requirement in Section 508 to code a Skip Navigation at the
My problematic code: testMYSQL=`mysql -u $mysqlUser -p$mysqlPass -h $mysqlHost --skip-column-names --batch -D $mysqlDB -e
I have the following code (calling a method returning IEnumerable): FibonacciWithLinq.Skip(delegate() { return 5;
I wrote a short bit of code to simply skip num_lines lines in an
I don't know how to code this but i want program to skip or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.