View Full Version : mysql_insert_id

02-16-2007, 06:22 PM
Hi all.

I am running a table to generate a serial number using auto_increment and unsigned zerofill. It's for filenames for uploaded pictures:

mysql_query("INSERT serial_gen SET serial=''");

$serial = mysql_insert_id();

copy ($_FILES['photo1']['tmp_name'], "userimages/".$serial.".jpg") or die ("Could not copy");

The thing is although the mysql table (viewed with phpmyadmin) will show leading zeros ie (000001 and 000002) on echoing out $serial I will just get (1) or (2) without the leading zeros.

Now I know I can, with strlen(), workout how many leading zeros I'm missing and insert them but I was wondering if there's something you can do/add to mysql_insert_id() to pull the zeros back for you.


02-16-2007, 06:30 PM
AFAIK mysql_insert_id doesn't provide formatting for the result it returns.

So... what's wrong with using strlen()?

02-16-2007, 06:41 PM
The PHP sprintf() function provides leading zero formating - http://us2.php.net/manual/en/function.sprintf.php

02-16-2007, 07:11 PM
Thanks for the replies.

So... what's wrong with using strlen()?

There's nothing wrong with it. I just assumed there would be a 'neater' way.

02-17-2007, 03:19 AM
Here is another way that might be more direct than using sprintf - http://www.php.net/manual/en/function.str-pad.php