...

View Full Version : mysql_insert_id



Cacus
02-16-2007, 05: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.


Cheers
Steve

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

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

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

Cacus
02-16-2007, 06: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.

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum