Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
use current mysql_insert_id within current query
Old 01-31-2010, 09:22 PM use current mysql_insert_id within current query
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
hi everybody

ive got a problem with a mysql_query and the only reliable way that i can think to get around it is if its possible to obtain and use the mysql_insert_id
of the current query within the query itself.

even if that is possible, im inserting multiple rows with one query, and i need to fill 1 column with a groupId (I.E an identifier for all the rows inserted in the query.) and another containing a copy of the id for the curent row, but placing it inside a JSON string. . .

i dont know if this is even possible, but here's an idea of what i need.

for example

Code:
insert into table (auto_inc, groupId, json) values
('',mysql_insert_id(of first row in query), '{\'reqId\':mysql_insert_id(thisRowid)}),
('',mysql_insert_id(of first row in query), '{\'reqId\':mysql_insert_id(thisRowid)}),
('',mysql_insert_id(of first row in query), '{\'reqId\':mysql_insert_id(thisRowid)})
the results that i would anticipate being in the table would be
Code:
auto_inc , groupId, json
1 , 1 ,{'reqId':1} 
2 , 1 ,{'reqId':2} 
3 , 1 ,{'reqId':3}

having thought about this for hours now, im begining to think that what i want is only possible with a long and potentially unreliable loop -

say i first insert a number of dead rows into my table, only filling the auto_inc column, then i get the id of the first inserted row of the query with mysql_insert_id(); and i obtain the amount of rows inserted with mysql_affected_rows().

With this information i could create a loop which begins at the mysql_insert_id() and iterates for mysql_affected_rows() as follows-

PHP Code:
$groupId=mysql_insert_id();
for(
$i=$groupid$i<($groupId+mysql_affected_rows()); $i++){
mysql_query("update table set groupId=$groupId, json='{\'reqId\':$i}' where auto_inc=$i");

anybody know a better way to achieve what im talking about with using multiple queries. i dont mind using 2 queries, one to insert and one to update, but i definately dont want to put a query inside a loop as above.

thanks for looking

mark
hiptobesquare is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-01-2010, 03:40 AM Re: use current mysql_insert_id within current query
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
No, it's impossible to do that in 1 pass.
Because, in your model, you would need the id of a value that is to be inserted.

What you need is to insert the rows, with empty group/json data, to have the ids, and then do a db update on the whole block to populate the needed infos.

Or, as you would in an enterprise class db, use a sequence or replace the auto_increment with a custom made counter, to have control over it.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-02-2010, 03:19 AM Re: use current mysql_insert_id within current query
Banned

Posts: 119
Trades: 0
Please let me know the error message.
Fusion BPO is offline
Reply With Quote
View Public Profile Visit Fusion BPO's homepage!
 
Old 02-02-2010, 05:24 AM Re: use current mysql_insert_id within current query
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://www.webmaster-talk.com/php-fo...increment.html

http://www.webmaster-talk.com/php-fo...n-a-query.html
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to use current mysql_insert_id within current query
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16603 seconds with 12 queries