-2

I'm making a data base and I would like to get two unique ids from each player like clash royale game .

1.id unique only number (It knows only the user)

2.id unique number and letters (all the players can see it).

I was thinking of using the time to get the first unique id, and then add a random number, but I think this would create a string that is too long. Moreover it does not guarantee 100% yet to obtain a unique id.

I'm working with PHP and MySQL

Jake
  • 1
  • 4
  • 2
    Welcome to StackOverflow! We will be glad to help you if you get stuck on a *specific* programming problem, but we are not here to write code or design your system for you. You will need to at least make an attempt at solving your own issue. Please see [**How do I ask a good question?**](https://stackoverflow.com/help/how-to-ask) and [**What topics can I ask about here?**](https://stackoverflow.com/help/on-topic). – Alex Howansky Mar 27 '18 at 15:10
  • Thanks to the immediate response,my specific problem is how to get two unique keys. this is not good? – Jake Mar 27 '18 at 15:12
  • Please see [**How do I ask a good question?**](https://stackoverflow.com/help/how-to-ask) and [**What topics can I ask about here?**](https://stackoverflow.com/help/on-topic). – Alex Howansky Mar 27 '18 at 15:15
  • I modified my post. I hope it goes better. I'm new I apologize if I do not work well yet. – Jake Mar 27 '18 at 15:20
  • 2
    Please post the PHP code you've already tried. For more general programming suggestions, there are other StackExchange sites for that. – Yserbius Mar 27 '18 at 15:37
  • I modified adding the idea of what I was thinking of doing – Jake Mar 27 '18 at 15:45
  • Why do you think they have to be different numbers for public/private use? A simple SQL auto incrementing column is generally sufficient. – ceejayoz Mar 27 '18 at 16:08
  • I think I use the private key for assistance and the public key to show the players records so that we avoid problems with players of the same name. do you find this wrong? – Jake Mar 27 '18 at 16:17
  • @Jake I do. Your key would be a unique, automatically incrementing number. There'd never be players with the same key. – ceejayoz Mar 27 '18 at 16:30
  • But I would like to prevent players from knowing their number. That's why I wanted to push the key out – Jake Mar 27 '18 at 16:35
  • use `mt_rand()` function or turn string into hex or binary – Richard Mar 27 '18 at 17:07

1 Answers1

0

https://secure.php.net/manual/en/function.uniqid.php

It won't return a numeric string, for that I guess you could use microtime plus a sufficiently long random number to limit the chances of collision to virtually nil. But why not use uniqid for both?

fred2
  • 1,015
  • 2
  • 9
  • 29
  • Using unique id is good but I get a very long string (23 for a strong security) and I fear this will affect performance. I was thinking of using a first numerical id because numeric values are faster to search than string values – Jake Mar 27 '18 at 15:59
  • @Jake `uniqid`, despite the name, doesn't guarantee uniqueness. That said, long strings aren't an issue as long as you index the column. – ceejayoz Mar 27 '18 at 16:09
  • Using a GUID as a primary key doesn't have to be a problem, lots of people do it on big databases. Just because it's a string to you doesn't mean it can't be stored as a number from MySQL's perspective: https://stackoverflow.com/questions/2365132/uuid-performance-in-mysql/2365176 – fred2 Mar 27 '18 at 16:10
  • Thanks for the link very useful but does not say how to turn a string to number from MySQL's perspective – Jake Mar 27 '18 at 16:25
  • Instead of storing the string as varchar or char or similar, use varbinary or binary. I have to say I haven't done it myself, but it makes sense to me in theory. See the comment by Kyle Rozendo in the link, and some more comment here: http://www.ovaistariq.net/632/understanding-mysql-binary-and-non-binary-string-data-types/#.WruqsXXwZhE – fred2 Mar 28 '18 at 14:49