onedumbcoder Posted June 3, 2009 Share Posted June 3, 2009 Is it possible to create a primary key id that is hexadecimal and auto incremental? I currently use numbers and I would like to know if it is possible to use hexadecimal id and have the table auto increment it. Otherwise I was thinking of using a 20 length string who's values would be randomly generate? for sample id[0] = rand(14), id[1] = rand(14), .... and so on? but i was thinking that would/might lead to problems? Quote Link to comment https://forums.phpfreaks.com/topic/160742-hexadecimal-id-instead-of-number/ Share on other sites More sharing options...
saeed_violinist Posted June 3, 2009 Share Posted June 3, 2009 the only problem with rand() is that a little chance to generate same number, if you write a simple script to check if the number is already in database or not, it is ok. Quote Link to comment https://forums.phpfreaks.com/topic/160742-hexadecimal-id-instead-of-number/#findComment-848413 Share on other sites More sharing options...
aschk Posted June 3, 2009 Share Posted June 3, 2009 My only question is ... why? Any integer (decimal) can be represented in HEX form, so why would you bother storing them in hex? And if you think about it... all numbers in any computer system are binary anyway... it's just the user representation that differs to make it more readable. So again, my question ... why? Quote Link to comment https://forums.phpfreaks.com/topic/160742-hexadecimal-id-instead-of-number/#findComment-848420 Share on other sites More sharing options...
aschk Posted June 3, 2009 Share Posted June 3, 2009 As a solution.... use integer (decimal) auto_increment column, and just use SELECT HEX(<column name here>) FROM <table name here>... easy. Quote Link to comment https://forums.phpfreaks.com/topic/160742-hexadecimal-id-instead-of-number/#findComment-848421 Share on other sites More sharing options...
onedumbcoder Posted June 3, 2009 Author Share Posted June 3, 2009 here is the issue I am. I have a page where the id of the element is in the url for example lets just say the url is www.somesite.com/viewitem.php?id=1 I dont want users to be able to view different items sequentially changing the id for example www.somesite.com/viewitem.php?id=1 www.somesite.com/viewitem.php?id=2 www.somesite.com/viewitem.php?id=3 www.somesite.com/viewitem.php?id=4 .... There are many reason I have for this, one is I dont want it to give away which items where added last what item came after item 5 lets say. I want to prevent them from being able to determine anything from the id. this is why I think if I that maybe the best way of doing this is by generating a random hex id. Quote Link to comment https://forums.phpfreaks.com/topic/160742-hexadecimal-id-instead-of-number/#findComment-848484 Share on other sites More sharing options...
michael624 Posted June 3, 2009 Share Posted June 3, 2009 when you create id each have to be unique so there are few ways to do this. You can hash(sha1, md5) the current time and thats the id, hashes look crazy and have no order. But they are lony like 30 characters. Or be simple and just put in the datetime using you own "secret way" like today is 06-03-09 4:40 so the id could be 3060440 and just add like 123456 to it and then you get 423896. Now tell me what date that represents? Since it invloves date and time it will always be different and it will be wierd because time changes. use seconds and then its super cyptic! Quote Link to comment https://forums.phpfreaks.com/topic/160742-hexadecimal-id-instead-of-number/#findComment-848812 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.