473,800 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1909
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
41949
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" that would be populated with a unique number upon insertion... How can I get the unique number it assigned to that record returned for futher use?
0
2399
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 . I've those two piece of code, the first is part of a class: --------- function connect($layer,$host,$db_port,$db_name,$user,$pass) { $dsn = "$layer://$user:$pass@$host:$db_port/$db_name";
2
1540
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 immediately need the auto_incremented Key-ID of the new record to put other information in related tables. In the past i've gotten that ID by just doing a SELECT with a WHERE that matches all the values i've just INSERTED, but it seems a cumbersome...
6
4691
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 by mysql_insert_id() function. However, the problem is that my web site can visit many users and many of them can initiate the describe procedure. It can happen that between the first user inserts something into database and executes...
3
2274
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 the initial insert by the first user but b4 the insert_id part can be queried. Regards
2
1533
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 offer foreign key feature on tables,so what I did is stick the corresponding submission id in the member's uploaded file when renaming the file. Each member can upload up to 6 files. Now,I'm clueless how to retrieve those matching images(or just 1...
3
12449
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 insert a record and return the inserted record id. (each record will insert by a unique id).
1
2920
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 autoincrement (my primary key) when using a stored procedure? Thanks.
3
2862
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) values('$name','value')”);
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.