Take the 2-minute tour ×

I have:

$token  = sha1(uniqid(mt_rand(), true));

Then I'll insert it into the database using unhex('$token'). How do I select this value?

I've tried:

SELECT token FROM `name` WHERE `id` = 1 LIMIT 1

If I echo it in it'll show as 0b7GÞ÷:‹xcÅ•½JAjdäáæ rather than something like 5dc012f007b7c4db7b9e076138fdd92fa1039530

What am I doing wrong?

share|improve this question
"What am I doing wrong?" - how about: you are using UNHEX in the first place? I mean, what for do you do that? And if you really think this is necessary - why haven't you read in the manual about what the "inverse" function to UNHEX is? –  CBroe Mar 17 '13 at 0:36
@CBroe stackoverflow.com/questions/614476/… –  duck Mar 17 '13 at 0:39

1 Answer 1

up vote 0 down vote accepted

I don't think you need to store it with UNHEX, but you can SELECT HEX(token) to get back the original value.

share|improve this answer
I figured out moments ago I had to do "HEX(token) as token", leaving out the as token was causing an undefined index error. Thanks. :) –  duck Mar 17 '13 at 0:37

Your Answer


