Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: mysql_insert_id

  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts

    mysql_insert_id

    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:

    Code:
    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

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    AFAIK mysql_insert_id doesn't provide formatting for the result it returns.

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

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    The PHP sprintf() function provides leading zero formating - http://us2.php.net/manual/en/function.sprintf.php
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    Here is another way that might be more direct than using sprintf - http://www.php.net/manual/en/function.str-pad.php
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •