Results 1 to 13 of 13

Thread: [RESOLVED] Auto_increment question

  1. #1

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Resolved [RESOLVED] Auto_increment question

    From the responses in this thread:
    http://www.vbforums.com/showthread.php?t=513904

    I'm doing a simple script for someone adding banners to their site:
    id
    banner_type
    file_name
    'etc...

    I want the file_name to always be unique. So I will append the id field to the beginning of the file name. But I won't know the ID until I have inserted the new record.

    What's the best way to do this? Insert the record with a blank file_name field, then use the mysql_insert_id() and then update that record after?

    A real quick example of what I'm talking about:
    PHP Code:
    <?php
      
    // Insert the record.
      
    mysql_query("INSERT into banners (id,banner_type,file_name,url,alt_text) VALUES
      (NULL,'1','','www.something.com','Visit something.com!')"
    );
      
      
    // Get ID of last insert.
      
    $insert_id mysql_insert_id($dbc); //<- $dbc is current connection value.
      
      // Get unique file name.
      
    $fname $insert_id "_$actual_file_name";
      
      
    // Update record.
      
    mysql_query("UPDATE banners SET file_name='$fname' WHERE id='$insert_id' LIMIT 1");
    ?>
    Is this the correct way to do it?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Auto_increment question

    The insert and update is a safe non-transactional method. I'll have to think a bit more to see if there is a more elegant way.

    (LAST_INSERT_ID() + 1) works to a degree, but only if you've already done an insert using that connection, and the auto_increment value hasn't been manually altered. This seems unacceptable.

    By the way, you can omit auto increment columns altogether from your INSERT query.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Auto_increment question

    If you were to use a transaction, you could use the SHOW TABLE STATUS query. To reduce code and overhead, I would write a stored procedure to do this.
    Last edited by penagate; Mar 29th, 2008 at 01:43 AM.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Auto_increment question

    Quote Originally Posted by DigiRev
    From the responses in this thread:
    http://www.vbforums.com/showthread.php?t=513904

    I'm doing a simple script for someone adding banners to their site:
    id
    banner_type
    file_name
    'etc...

    I want the file_name to always be unique. So I will append the id field to the beginning of the file name. But I won't know the ID until I have inserted the new record.

    What's the best way to do this? Insert the record with a blank file_name field, then use the mysql_insert_id() and then update that record after?

    A real quick example of what I'm talking about:
    PHP Code:
    <?php
      
    // Insert the record.
      
    mysql_query("INSERT into banners (id,banner_type,file_name,url,alt_text) VALUES
      (NULL,'1','','www.something.com','Visit something.com!')"
    );
      
      
    // Get ID of last insert.
      
    $insert_id mysql_insert_id($dbc); //<- $dbc is current connection value.
      
      // Get unique file name.
      
    $fname $insert_id "_$actual_file_name";
      
      
    // Update record.
      
    mysql_query("UPDATE banners SET file_name='$fname' WHERE id='$insert_id' LIMIT 1");
    ?>
    Is this the correct way to do it?
    Why do you want the file name to be unique yet at the same time you are appending a unique value to the string? It makes no sense. Remove the unique constrain on the file name as you clearly do not need it.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Auto_increment question

    i would just make a random hash and make that the file name

    using rand() you can do many things with it.

    EXAMPLES
    PHP Code:
    //simple - create a random number between 100 and 99999
    $id rand(100,99999);

    //multiplying - multiply random numbers together
    $id rand(100,99999)*rand(1,999);

    //using MD5 with it makes it more unique
    $id md5(rand(100,99999));

    //using date function:
    $id md5(date('r')); 
    My usual boring signature: Something

  6. #6

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Auto_increment question

    Quote Originally Posted by visualAd
    Why do you want the file name to be unique yet at the same time you are appending a unique value to the string? It makes no sense. Remove the unique constrain on the file name as you clearly do not need it.
    I don't understand what you mean?

    He uploads a file which is a banner image. The name of the file needs to be unique so it never overwrites the image that is already on his server. I'd rather not ask him to change the name of the image if one already exists, so I want it to always be unique.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Auto_increment question

    Quote Originally Posted by dclamp
    i would just make a random hash and make that the file name
    Not guaranteed to be unique.

  8. #8

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Auto_increment question

    penagate - I've never used stored procedures but I'm reading into it...

    Quote Originally Posted by dclamp
    i would just make a random hash and make that the file name

    using rand() you can do many things with it.

    EXAMPLES
    PHP Code:
    //simple - create a random number between 100 and 99999
    $id rand(100,99999);

    //multiplying - multiply random numbers together
    $id rand(100,99999)*rand(1,999);

    //using MD5 with it makes it more unique
    $id md5(rand(100,99999));

    //using date function:
    $id md5(date('r')); 
    Yeah I guess using the md5(date) would always be unique, not sure why I didn't think of this, might be easier. But what if the date gets reset on his server or something? That could cause problems...

  9. #9
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Auto_increment question

    then try this:

    PHP Code:
    //using date function:
    $datehash date('r');
    $randhash rand(100,99999)*rand(1,999); 

    $id $datehash $randhash;
    $id md5($id); 
    My usual boring signature: Something

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Auto_increment question

    How can it be more unique? It's either unique or it isn't. Checksums are not unique. They are designed to be used for verification when combined with other data metrics.

  11. #11
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Auto_increment question

    Quote Originally Posted by DigiRev
    I don't understand what you mean?

    He uploads a file which is a banner image. The name of the file needs to be unique so it never overwrites the image that is already on his server. I'd rather not ask him to change the name of the image if one already exists, so I want it to always be unique.
    I apologise. I misunderstood you.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  12. #12

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Auto_increment question

    No problem visual

    I'm sure using the hashes there's like a 0.000001% chance it will fail but I'd rather just do it the "correct" way and not have to worry about it ever failing. I'm kind of a perfectionist...

    His MySQL version is not up-to-date either and I think stored procedures are not supported with his version.

    Should I stick with the way I'm doing it?

  13. #13
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Auto_increment question

    INSERT --> UPDATE is fine as the operation of saving the file is dependant on the operation of adding the record. You could however use the previous ID column - but that would again rely on a second query.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width