Insertion Query into MySQL

Hi,
Wonder if anyone can help.

I have a table I'd like to insert a row into using PHP. This table will be being used by a CMS, so at any point any no. of users could be inserting rows into it.

Once the code has done the insert, I'd like to get the recordID of the new row.
So I could: do a select on the table immediately following the insert to get the new recordID.

I can't 100% rely upon any auto-increment feature since someone else could have inserted a new row in between my inserting, then finding the new recordID using auto-increment.

Is there anything very handy in PHP like:

newRecordID = php_insert_row (insert parameters ....)

that returns the new record ID for me without relying upon auto incremenet and one that would mean I wouldn't have to write a cumbersome select query immediately post the insert?

This is PHP4 running wtih MySQL 3.x.

Thanks for any help,
Ben.











bcopsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hernst42Commented:
If that id-column has auto_increment enabled you can use
mysql_insert_id() to get that id. There is no need to do a select afterwards.

mysql_insert_id will give you the id that the current connection has inserted, you won't get the id of the insert from someone other.

See http://de3.php.net/manual/en/function.mysql-insert-id.php
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nesnemisCommented:
Hi bcops,

to get the id use:
    $id = mysql_insert_id($sqlInsertStatement);

nesnemis
0
eeBlueShadowCommented:
From the MYSQL documentation pages:
http://dev.mysql.com/doc/mysql/en/Getting_unique_ID.html
--------
When a new AUTO_INCREMENT value has been generated, you can also obtain it by executing a SELECT LAST_INSERT_ID() statement mysql_query() and retrieving the value from the result set returned by the statement.

For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It will not be changed by another client. It will not even be changed if you update another AUTO_INCREMENT column with a non-magic value (that is, a value that is not NULL and not 0).
--------

So, follow your INSERT SQL statement with a "SELECT LAST_INSERT_ID()" and you'll be fine

_Blue
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.