473,231 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

mysql_insert_id

how could i reduce the following piece of code:

for ($num=0;$num<50;$num++) {
mysql_query($db,"INSERT INTO whatever (stuff) VALUES (somevalue);";
mysql_query($db,"INSERT INTO sometable VALUES
(".mysql_insert_id().");";
}

to something where only two sql queries are made (as opposed to 100?).
ie. where the first line could be converted to something like this:

mysql_query($db,"INSERT INTO whatever (stuff) VALUES
(".implode(",",somearray).")";

where somearray contains every somevalue.
Jul 17 '05 #1
1 1886
Carved in mystic runes upon the very living rock, the last words of
yawnmoth of comp.lang.php make plain:
how could i reduce the following piece of code:

for ($num=0;$num<50;$num++) {
mysql_query($db,"INSERT INTO whatever (stuff) VALUES (somevalue);";
mysql_query($db,"INSERT INTO sometable VALUES
(".mysql_insert_id().");";
}


I don't know if you're aware of extended insert format:

insert into Table (f1, f2) values (v1a, v2a), (v1b, v2b)....

However, mysql_insert_id() will only return the value for the first
record. What you could do, though, is this:

$FirstID = mysql_insert_id();
$Query = "select ID from Table where ID>=$FirstID limit 0, $NumRows";

Where $NumRows is the number of rows you inserted. Since all the rows
were inserted by one query, they will have consecutive IDs.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sugapablo | last post by:
Let's say I create a new record in a table like this: mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn); ....that had an auto-incrementing, unique identifying column named "ID"...
0
by: kain | last post by:
hi there, I'm currently converting a MySQL application to PostgreSQL, and I hacked this app to support PEAR::Db . after some good coding days, I've found a problem: mysql_insert_id on pear::db ....
2
by: Ravenslay3r | last post by:
I'm making a web-site thats backended by php-2-mysql. A common problem seems to be when processing form data, I use information early in the form to create a new record in a main table. I then...
6
by: opt_inf_env | last post by:
Hi, I would like to insert a data (row) into database (MySQL) and than to generate file whose name contains the ID of the inserted row (ID is declared as AUTO_INCREMENT). I wanted to extract ID...
3
by: monomaniac21 | last post by:
Hi all With regard to mysql's mysql_insert_id function is it possible that the query can return the insert id from another insert performed by a another user of the database which occured after...
2
by: Jankie | last post by:
Hi and wish all moderators and members are doing well i Back with a question after a long while,hope to find the valuable typical support from thescripts people and members. My host does not...
3
by: svsenthilkumar | last post by:
Hai, i want to insert a record and retrieve id using php-mysql. i want insert a record by copy of another one table. after inserting i update a field in the inserted record. so, can i...
1
by: Fred | last post by:
if my query is a regular INSERT, then mysql_insert_id() works as expected. But if I use a Stored Procedure to do my insert, then mysql_insert_id() returns 0 How can I get a returned...
3
by: vjayis | last post by:
hi i have a form which inserts records into my table using php and to get the inserted id(auto_incremented ID) can i use this command? <? mysql_query(“insert into table(name,value)...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.