472,117 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

reading auto increment number before it is written?

Ok, this is a stupid problem, I admit. I have a scrip that adds records to
a table. The records are for photos. So, after adding a record the scrip
also uploads a picture from users computer and renames it to
{$recordID}.jpg.

The problem is, my recordID field is auto incrementing in mySQL. So
currently I just take the last added record and assume that it the one I
just added, and rename the file to the recordID of that record. It works,
but clearly isn't the best way to do this as 2 users can add records at the
same time and then I would have a problem.

So what I am wondering is if there is a way to write a record to mySQL and
then have mySQL return recordID of the record just written?

- Bogdan
Jul 14 '06 #1
2 2947
>From the docs:

LAST_INSERT_ID([expr])
Returns the last automatically generated value that was inserted into
an AUTO_INCREMENT column. See section 8.1.3.130 mysql_insert_id().
mysqlSELECT LAST_INSERT_ID();
-195

The last ID that was generated 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). If you
insert many rows at the same time with an insert statement,
LAST_INSERT_ID() returns the value for the first inserted row. The
reason for this is to make it possible to easily reproduce the same
INSERT statement against some other server. If expr is given as an
argument to LAST_INSERT_ID(), then the value of the argument is
returned by the function, and is set as the next value to be returned
by LAST_INSERT_ID(). This can be used to simulate sequences: First
create the table:
mysqlCREATE TABLE sequence (id INT NOT NULL);
mysqlINSERT INTO sequence VALUES (0);

Then the table can be used to generate sequence numbers like this:
mysqlUPDATE sequence SET id=LAST_INSERT_ID(id+1);

You can generate sequences without calling LAST_INSERT_ID(), but the
utility of using the function this way is that the ID value is
maintained in the server as the last automatically generated value
(multi-user safe). You can retrieve the new ID as you would read any
normal AUTO_INCREMENT value in MySQL. For example, LAST_INSERT_ID()
(without an argument) will return the new ID. The C API function
mysql_insert_id() can also be used to get the value. Note that as
mysql_insert_id() is only updated after INSERT and UPDATE statements,
so you can't use the C API function to retrieve the value for
LAST_INSERT_ID(expr) after executing other SQL statements like SELECT
or SET.

x0054 wrote:
Ok, this is a stupid problem, I admit. I have a scrip that adds records to
a table. The records are for photos. So, after adding a record the scrip
also uploads a picture from users computer and renames it to
{$recordID}.jpg.

The problem is, my recordID field is auto incrementing in mySQL. So
currently I just take the last added record and assume that it the one I
just added, and rename the file to the recordID of that record. It works,
but clearly isn't the best way to do this as 2 users can add records at the
same time and then I would have a problem.

So what I am wondering is if there is a way to write a record to mySQL and
then have mySQL return recordID of the record just written?

- Bogdan
Jul 14 '06 #2
"ImOk" <jo**********@gmail.comwrote in
news:11**********************@m79g2000cwm.googlegr oups.com:
>>From the docs:

LAST_INSERT_ID([expr])
Returns the last automatically generated value that was inserted into
an AUTO_INCREMENT column. See section 8.1.3.130 mysql_insert_id().
mysqlSELECT LAST_INSERT_ID();
-195

The last ID that was generated 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).
If you insert many rows at the same time with an insert statement,
LAST_INSERT_ID() returns the value for the first inserted row. The
reason for this is to make it possible to easily reproduce the same
INSERT statement against some other server. If expr is given as an
argument to LAST_INSERT_ID(), then the value of the argument is
returned by the function, and is set as the next value to be returned
by LAST_INSERT_ID(). This can be used to simulate sequences: First
create the table:
mysqlCREATE TABLE sequence (id INT NOT NULL);
mysqlINSERT INTO sequence VALUES (0);

Then the table can be used to generate sequence numbers like this:
mysqlUPDATE sequence SET id=LAST_INSERT_ID(id+1);

You can generate sequences without calling LAST_INSERT_ID(), but the
utility of using the function this way is that the ID value is
maintained in the server as the last automatically generated value
(multi-user safe). You can retrieve the new ID as you would read any
normal AUTO_INCREMENT value in MySQL. For example, LAST_INSERT_ID()
(without an argument) will return the new ID. The C API function
mysql_insert_id() can also be used to get the value. Note that as
mysql_insert_id() is only updated after INSERT and UPDATE statements,
so you can't use the C API function to retrieve the value for
LAST_INSERT_ID(expr) after executing other SQL statements like SELECT
or SET.

x0054 wrote:
>Ok, this is a stupid problem, I admit. I have a scrip that adds
records to a table. The records are for photos. So, after adding a
record the scrip also uploads a picture from users computer and
renames it to {$recordID}.jpg.

The problem is, my recordID field is auto incrementing in mySQL. So
currently I just take the last added record and assume that it the
one I just added, and rename the file to the recordID of that record.
It works, but clearly isn't the best way to do this as 2 users can
add records at the same time and then I would have a problem.

So what I am wondering is if there is a way to write a record to
mySQL and then have mySQL return recordID of the record just written?

- Bogdan

Thank you very much, that's exactly what I was looking for.

- Bogdan
Jul 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by opt_inf_env | last post: by
2 posts views Thread by Tom | last post: by
1 post views Thread by John | last post: by
13 posts views Thread by S.Dickson | last post: by

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.