469,323 Members | 1,429 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

LAST_INSERT_ID with ZEROFILL

I am losing the leading zeros when I get autoincrement IDs from a
column with ZEROFILL enabled. I would like to have the leading
zeros. I have tried both the php function last_insert_id as well as
running a SELECT query and using the result. I am getting the correct
id, it is just losing the leading zeros. I have doulbe checked the
table in the database, and it does have the leading zeros. Thanks for
any help.

$result = mysql_query($insertQuery);
if($result)
{
//$uniqueFilenameResult = mysql_insert_id($link).$uploadFilename;
$insertIDResult = mysql_query("SELECT LAST_INSERT_ID()");
if($insertIDResult)
{
$nrows = mysql_num_rows($insertIDResult);
$rows = mysql_fetch_row($insertIDResult);
$uniqueFilenameResult = $rows[0].$filename;
}
}

Oct 18 '07 #1
2 2692
..oO(WebSnozz)
>I am losing the leading zeros when I get autoincrement IDs from a
column with ZEROFILL enabled.
I wouldn't rely on that.
>I would like to have the leading
zeros. I have tried both the php function last_insert_id as well as
running a SELECT query and using the result. I am getting the correct
id, it is just losing the leading zeros. I have doulbe checked the
table in the database, and it does have the leading zeros. Thanks for
any help.
Just store normal integers and add the leading zeros when you really
need them, for example with sprintf() in your script

Micha
Oct 18 '07 #2
On Thu, 18 Oct 2007 18:45:25 +0000, WebSnozz wrote:
I am losing the leading zeros when I get autoincrement IDs from a
column with ZEROFILL enabled. I would like to have the leading
zeros. I have tried both the php function last_insert_id as well as
running a SELECT query and using the result. I am getting the correct
id, it is just losing the leading zeros. I have doulbe checked the
table in the database, and it does have the leading zeros. Thanks for
any help.

$result = mysql_query($insertQuery);
if($result)
{
//$uniqueFilenameResult = mysql_insert_id($link).$uploadFilename;
$insertIDResult = mysql_query("SELECT LAST_INSERT_ID()");
if($insertIDResult)
{
$nrows = mysql_num_rows($insertIDResult);
$rows = mysql_fetch_row($insertIDResult);
$uniqueFilenameResult = $rows[0].$filename;
}
}
You can use str_pad() to fill it back out.

$insertIDResult = mysql_query("SELECT LAST_INSERT_ID()");
$insertIDResult = str_pad($insertIDResult, N, "0", STR_PAD_LEFT);

Where N is the desired length of the string.

--
Kelsey Sigurdur

Oct 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Irene Mettias | last post: by
1 post views Thread by Michal Adamczakk | last post: by
6 posts views Thread by jim | last post: by
3 posts views Thread by Khazret Sapenov | last post: by
3 posts views Thread by Vic Spainhower | last post: by
2 posts views Thread by NotVeryBright | last post: by
3 posts views Thread by jx2 | last post: by
1 post views Thread by sonia.sardana | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.