forumnz Posted April 26, 2009 Share Posted April 26, 2009 I want to choose a random number and convert it to an md5 string inside the db query. This is what I have: mysql_query("INSERT INTO chart_of_accs (hash_key, acc_hash, type, code, name, description, tax, show_exp_claims, sys_locked) VALUES ('$demo_hash', '(md5(rand() + rand() + rand()))', 'r_r', '200', 'Sales', 'Income', 'gst_n', '1', '0')"); In the DB it comes up with (md5(rand() + rand() + rand())) What can I do to make it work? Thanks Sam Quote Link to comment https://forums.phpfreaks.com/topic/155761-solved-how-can-i-insert-an-md5-into-database/ Share on other sites More sharing options...
Bizty Posted April 26, 2009 Share Posted April 26, 2009 is it vital you have it in the query? else you could just pop it in a $var instead. Quote Link to comment https://forums.phpfreaks.com/topic/155761-solved-how-can-i-insert-an-md5-into-database/#findComment-819898 Share on other sites More sharing options...
.josh Posted April 26, 2009 Share Posted April 26, 2009 md5 and rand are php functions. They need to be outside of your query string quote so php will parse it. Quote Link to comment https://forums.phpfreaks.com/topic/155761-solved-how-can-i-insert-an-md5-into-database/#findComment-819900 Share on other sites More sharing options...
ToonMariner Posted April 26, 2009 Share Posted April 26, 2009 <?php mysql_query("INSERT INTO chart_of_accs (hash_key, acc_hash, type, code, name, description, tax, show_exp_claims, sys_locked) VALUES ('$demo_hash', MD5(RAND() + RAND() + RAND()), 'r_r', '200', 'Sales', 'Income', 'gst_n', '1', '0')"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/155761-solved-how-can-i-insert-an-md5-into-database/#findComment-819901 Share on other sites More sharing options...
coalgames Posted April 26, 2009 Share Posted April 26, 2009 Also, another tip is dont use (rand() + rand() + rand()). Instead, you could concatenate it into (rand() . rand() . rand()). Adding results into a number such as 1.215972. Concatenating will turn it into 0.37496290.293623890.239672 which is more random. mysql_query("INSERT INTO chart_of_accs (hash_key, acc_hash, type, code, name, description, tax, show_exp_claims, sys_locked) VALUES ('$demo_hash', '" . md5(rand() . rand() . rand()) . "', 'r_r', '200', 'Sales', 'Income', 'gst_n', '1', '0')"); This should probably work because you are exiting text mode and using the function. Quote Link to comment https://forums.phpfreaks.com/topic/155761-solved-how-can-i-insert-an-md5-into-database/#findComment-819909 Share on other sites More sharing options...
ToonMariner Posted April 27, 2009 Share Posted April 27, 2009 you don't need to generate that in php MD5(RAND()+RAND()+RAND()) will work fine in a query.... Quote Link to comment https://forums.phpfreaks.com/topic/155761-solved-how-can-i-insert-an-md5-into-database/#findComment-819937 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.