469,625 Members | 1,066 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

copy auto_increment value to another field

Hi,

i want to create a unique serial number to my Db entries. I thought
the best way would be to add the auto_increment primary key value to a
string, then insert it into a table field 'serial_num'.

$serial_num = "NAME000" . $auto_increment_value;

Is this possible in one function? Or do I have to insert all the other
data first, then get the last inserted id value and then update the
entry with the $serial_num variable?

Cheers
Jul 17 '05 #1
5 2870
Nel
"Paul Lamonby" <pa**@popimages.com> wrote in message
news:86**************************@posting.google.c om...
Hi,

i want to create a unique serial number to my Db entries. I thought
the best way would be to add the auto_increment primary key value to a
string, then insert it into a table field 'serial_num'.

$serial_num = "NAME000" . $auto_increment_value;

Is this possible in one function? Or do I have to insert all the other
data first, then get the last inserted id value and then update the
entry with the $serial_num variable?

Cheers


Hi Paul,

If you set one field type, lets call it "id", to an INT and set
auto_increment, primary, unique, this would automatically create a unique
number within the "id" field for all entries. You can the prefix the id
number with "NAME"+$id within php to achieve the above.

It really depends on what you want the serial number for, human readable
stuff or just passing on id within your site(s). You could use an md5()
hash of your number + some other variable to provide you with a more secure
id.

Nel.
Jul 17 '05 #2
"Nel" <ne***@ne14.co.NOSPAMuk> wrote in message news:<jK*******************@wards.force9.net>...
"Paul Lamonby" <pa**@popimages.com> wrote in message
news:86**************************@posting.google.c om...
Hi,

i want to create a unique serial number to my Db entries. I thought
the best way would be to add the auto_increment primary key value to a
string, then insert it into a table field 'serial_num'.

$serial_num = "NAME000" . $auto_increment_value;

Is this possible in one function? Or do I have to insert all the other
data first, then get the last inserted id value and then update the
entry with the $serial_num variable?

Cheers


Hi Paul,

If you set one field type, lets call it "id", to an INT and set
auto_increment, primary, unique, this would automatically create a unique
number within the "id" field for all entries. You can the prefix the id
number with "NAME"+$id within php to achieve the above.

It really depends on what you want the serial number for, human readable
stuff or just passing on id within your site(s). You could use an md5()
hash of your number + some other variable to provide you with a more secure
id.

Nel.


Nel,

Cheers for the reply, I have got it sorted, but my post was more
concerned with the 'correct way' to do this.

I have my auto_increment, primary, unique 'id' field and my 'serial'
field.
What I am doing is INSERTing data into the Db, then retrieving the
mysql_insert_id(), then UPDATEing the same entry with the $serial
prefixed variable, $serial = "NAME000" . $id;

Is the the correct way to do it? Or can I retrieve the id in the same
INSERT statement? Or can mysql add a prefix to a auto_increment,
primary, unique field?

Paul
Jul 17 '05 #3
In article <86**************************@posting.google.com >, Paul Lamonby wrote:
"Nel" <ne***@ne14.co.NOSPAMuk> wrote in message news:<jK*******************@wards.force9.net>...
What I am doing is INSERTing data into the Db, then retrieving the
mysql_insert_id(), then UPDATEing the same entry with the $serial
prefixed variable, $serial = "NAME000" . $id;


I presume it will work. But i don't see why you want to add a prefix to
the key? To me, it seems that that prefix is quite redundant.

I haven't had the time to check it out, but i think you might notice a
difference in speed (i know there is no theoretical base for this, but
it might to try it out in the real world) if you use an INT instead of
a CHAR for your primary key.

--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Jul 17 '05 #4
Tim Van Wassenhove <eu**@pi.be> wrote in message news:<2h************@uni-berlin.de>...
In article <86**************************@posting.google.com >, Paul Lamonby wrote:
"Nel" <ne***@ne14.co.NOSPAMuk> wrote in message news:<jK*******************@wards.force9.net>...
What I am doing is INSERTing data into the Db, then retrieving the
mysql_insert_id(), then UPDATEing the same entry with the $serial
prefixed variable, $serial = "NAME000" . $id;


I presume it will work. But i don't see why you want to add a prefix to
the key? To me, it seems that that prefix is quite redundant.

I haven't had the time to check it out, but i think you might notice a
difference in speed (i know there is no theoretical base for this, but
it might to try it out in the real world) if you use an INT instead of
a CHAR for your primary key.


Tim,

I know it seems strange, but I need this unique 'serial number' for
people to enter in via SMS to download an image from the server. I
could easily use the auto_increment primary key value, but people just
entering '1, 2, 3.. 34' as a serial number doesnt really have the same
kudos as a prefixed string, like IMAGE0023.

My post was wondering whether it is possible to set up a field in
mysql that auto_increments onto a string prefix, instead of having to
retrieve the id value and add it into another field, but it seems this
is not possible, so i have opted to retrieve the value and add it
manually.

Cheers
Jul 17 '05 #5
I noticed that Message-ID:
<86**************************@posting.google.com > from Paul Lamonby
contained the following:
My post was wondering whether it is possible to set up a field in
mysql that auto_increments onto a string prefix, instead of having to
retrieve the id value and add it into another field, but it seems this
is not possible, so i have opted to retrieve the value and add it
manually.


Wouldn't it be just as easy to get the system to ignore any prefix?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Pjotr Wedersteers | last post: by
4 posts views Thread by jjliu | last post: by
9 posts views Thread by Bart Van der Donck | last post: by
reply views Thread by Shailesh | last post: by
2 posts views Thread by hjyn | last post: by
1 post views Thread by Phil Latio | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.