1

I have read many answers on stackoverflow, but I haven't find anything related my issue.

This is my table:

`id` char(11) NOT NULL,
`element` varchar(32) NOT NULL,

I need to use an autoincremented unique string id of 11 chars ( case sensitive if possible ) and numbers as youtube does:

youtube.com/watch?v=j5syKhDd64s

youtube.com/watch?v=YVkUvTmDf3Y

youtube.com/watch?v=8BcDeoKLsaY

...

How could I do this with mysql/php ?

16
  • 6
    How can you increment a string? Commented Sep 1, 2015 at 9:27
  • 2
    @D4V1D The same way you could increment numbers. Heck, run-of-the-mill hexadecimal numbers are "incrementable strings": FE + 1 = FF. Commented Sep 1, 2015 at 9:28
  • @D4V1D aaaaaaaaaaa, aaaaaaaaaab, aaaaaaaaaac, aaaaaaaaaad, ... Commented Sep 1, 2015 at 9:29
  • This might help stackoverflow.com/questions/3567180/… Commented Sep 1, 2015 at 9:30
  • 1
    Is the Youtube reference random, or is there a pattern to it? Is it just a hash of an integer for example? Commented Sep 1, 2015 at 9:35

3 Answers 3

2

You probably want something like hashids. Their page also links to alternative solutions.

If that doesn't fit the bill, please describe your problem in more detail.

Sign up to request clarification or add additional context in comments.

5 Comments

it seems great but where can I put the last id to create the next row in mysql ?
You just use an integer auto incremented field for the id, and hash it for external use.
@user5287961 sorry I just noticed that it's not auto-incremented.
@Kickstart and for internal use ? Is there a reverse function to get the string from the id ?
The page given by user5287961 does talk about allowing decoding (and I would assume you will use a salt value to encode / decode them). But personally I would be tempted to store the encoded version on you database table as well.
0

one way to do it is to use this function

    function generateRandomString($length = 11) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

and then pass $randomString result to your query to add in DB.

EDIT : if OP is looking for the string like he mentioned (aaaaaaaaaaa, aaaaaaaaaab, aaaaaaaaaac, aaaaaaaaaad) i recommend you to check out Theory of Computation .

1 Comment

That's a terrible random string generator. rand is just about the worst random number generator you can choose. Use /dev/random to read a bunch of random bytes and simply convert them to a narrower alphabet. Base64 would be a decent choice.
0

This is my code which I Autoincremented my unique id string field.

case "Add":
    $itemno = $_POST['itemno'];
    $qty = $_POST['qty'];
    $unitprc = $_POST['unitprc'];
    $amt = $_POST['amt'];
    $coopmemid = $_SESSION['kiosk']['is_coopmemID_kiosk'];
    $totamt = 0;
    $totitm = count($itemno);
    $a_empgroid = array();
    for($x=0; $x<$totitm; $x++) {
        $Addquery = "INSERT INTO tb_empgrocery (coopmemID , date_ordered, item_no, qty_ordered, unit_price, amount) 
                     VALUES ('$coopmemid',(NOW()),'$itemno[$x]','$qty[$x]','$unitprc[$x]','$amt[$x]')";
        $atecCoop->query($Addquery);
        $totamt+=$amt[$x];
        $inserted_id = $atecCoop->insert_id;
        array_push($a_empgroid,$inserted_id);
    }
    $Savequery = "INSERT INTO tb_empgroc_master (order_status, date_ordered, total_items, total_amount) VALUES ('Pending', (NOW()), '$totitm', '$totamt')";
    $atecCoop->query($Savequery);
    $empgrocmstid = $atecCoop->insert_id;
    $orderno = date('y-m-').str_pad($empgrocmstid, 10, "0", STR_PAD_LEFT);
    $sql = "UPDATE tb_empgroc_master SET order_no='$orderno' WHERE empgrocmstID='$empgrocmstid'";
    $atecCoop->query($sql);
    foreach($a_empgroid as $empgrocid) {
        $sql = "UPDATE tb_empgrocery SET order_no='$orderno' WHERE empgrocID='$empgrocid'";
        $atecCoop->query($sql);
    }
break;

As you can see the field oder_no is a unique id (varchar 25)

Hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.