473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 2936
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Irene Mettias | last post by:
Hello, I've just installed perl DBI v1.38 which according to the documentation supports the method last_insert_id(). For some reason the return value I get is undef. Here is my source code :...
1
by: Michal Adamczakk | last post by:
hi, how to implement mysql's last_insert_id() ? i know that seqences and oids are great. the one thing i miss is that they are not session specific. i mean selecting last_value from seqence...
6
by: jim | last post by:
Hi, I have a site with various parts which allow users to enter info via forms which gets entered into various tables in a MySQL db (I'm using ASP, rather than PHP). I have an ecards bit, and...
3
by: Khazret Sapenov | last post by:
Hi, What is the practical use of ZEROFILL in your case? regards, Khaz
3
by: Vic Spainhower | last post by:
Hello, I am trying to get the value of the Auto-Incremented key that was assigned using LAST_INSERT_ID(). However, when I execute the following PHP statements after the insert I only get: "value...
2
by: NotVeryBright | last post by:
Hello I'm trying to create a table as below CREATE TABLE MyTable (ID FLOAT NOT NULL PRIMARY KEY ZEROFILL, Forename VARCHAR(30),Surname VARCHAR(30)); If I remove ZEROFILL it works ok But with...
1
by: simbarashe | last post by:
I am trying to insert an auto_increment value into multiple tables but its seems to lose its value after the the second insert statement and my code is as follows can any1 please help. The...
3
by: jx2 | last post by:
hi guys i would appriciate your coments on this code - when i ran it for the very first time it doesnt see @last = LAST_INSERT_ID() but when i ran it next time it read it properly i need to know it...
1
by: sonia.sardana | last post by:
Hi frnds, I want to know that zerofill is a valid function in SQL or not....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.