1

Suppose I have a table like this

----Id----Product----RandomKey--
|   1   |   P1     |  (some key) |
----------------------------------
|   2   |   P2     |  (some key) |
----------------------------------
|   3   |   P3     |  (some key) |
----------------------------------

Here the some key is unique random key

I want to generate randome string from base_convert. Mean check the previous id suppose 3 and then increment 1 with the value and convert it with using base_convert

$rowid=3; //Get the last id from database 
$bnum = base_convert($rowid,36,10);
$rkey = base_convert($bnum+1,10,36);
echo $rkey;

Does it always generate randome unique key? I really don't understand formbase and tobase in base_convert syntax

base_convert(number,frombase,tobase);

Can any one explain me how frombase and tobase work and by this method can i generate unique key?

Update:

If i set

$rkey = base_convert($bnum+1,10,36);

It will generate flat number, i want number with character. So I changed 36 to 16 mean Hexadecimal like

$rkey = base_convert($bnum+1,10,16);

and it will produce random string with number and character. If anyone recommend something better please let me know. Thanks

Ashis Biswas
  • 747
  • 11
  • 28
  • There are so many questions.. Basicaly - what ist the purpose? Do you just need a short representation of the *Id*? Does it need to be random, and why (what you have tried isn't random at all)? – Paul Spiegel Dec 26 '15 at 22:05
  • I want to create an string it may be 1 digit or more than 1 digit. I want to convert row id to a unique key(may be 1 or more than 1 digit). If i set 16 to tobase, it will generate alpha numeric character and will look like some random key. I want to create it only because of uniqueness. It will always unique If i am not wrong. – Ashis Biswas Dec 27 '15 at 04:43

1 Answers1

0

Can any one explain me how frombase and tobase work and by this method can i generate unique key?

Function base_convert do not provide generator for unique strings. The purpose of using this function is converting between numeral systems.

Example:

$randInt = 10; // we pick up a random integer value
$convertedRandInt = base_convert($randInt, 10, 2); // we converting it

var_dump($convertedRandInt); // output: 00001010

In above example, I used this function to convert between decimal numeral system to binary.

string base_convert ( string $number , int $frombase , int $tobase )

Fist parameter is string or number (because chars has their numeric sign, the second is current numeral system of variable (in the example I used decimal) and the third argument is numeral system what we are looking for (in the example, binary).

If anyone recommend something better please let me know. Thanks

About uniqueness, look at the answers on older topic: PHP: How to generate a random, unique, alphanumeric string?.

Community
  • 1
  • 1
Grzegorz Gajda
  • 2,424
  • 2
  • 15
  • 23