BrettHartel Posted December 4, 2012 Share Posted December 4, 2012 (edited) Thank you for taking the time to help me. I am trying to generate a string of sixteen characters that can look like 0000000000000000 to 9999999999999999 and set it as the global variable of $User_ID Here is the part of the code I am having trouble with: function genRandomString() { global $User_ID; $length = 16; $characters = "0123456789"; for ($p = 0; $p < $length; $p++) { $User_ID .= $characters[mt_rand(0, strlen($characters))]; } } Here is my whole code: <?php $con = mysql_connect("localhost","******","******"); if (!$con){ die('Could not connect: . mysql_error()'); } mysql_select_db("******", $con); echo "Connected to: " . $con; $eMail="$_POST[eMail]";$User_ID=""; echo "<br>My eMail: " . $eMail; $eMailResult = mysql_query("SELECT * FROM eMail WHERE eMail='$eMail'"); if (mysql_num_rows($eMailResult) == 0){ echo "<br>eMail is Unique"; } else { echo "eMail is NOT Unique<br>";} function genRandomString() { global $User_ID; $length = 16; $characters = "0123456789"; for ($p = 0; $p < $length; $p++) { $User_ID .= $characters[mt_rand(0, strlen($characters))]; } } echo "<br>User ID: " . $User_ID; mysql_close($con); ?> Edited December 4, 2012 by BrettHartel Quote Link to comment https://forums.phpfreaks.com/topic/271582-creating-a-random-string-of-16-characters-and-set-as-a-global-variable/ Share on other sites More sharing options...
Muddy_Funster Posted December 4, 2012 Share Posted December 4, 2012 do you have a bloody good reason for using a global? That aside - what's the actual problem? Quote Link to comment https://forums.phpfreaks.com/topic/271582-creating-a-random-string-of-16-characters-and-set-as-a-global-variable/#findComment-1397421 Share on other sites More sharing options...
BrettHartel Posted December 4, 2012 Author Share Posted December 4, 2012 Thank you Muddy_Funster but I figured it out. I can use this: $User_ID=rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9); Quote Link to comment https://forums.phpfreaks.com/topic/271582-creating-a-random-string-of-16-characters-and-set-as-a-global-variable/#findComment-1397430 Share on other sites More sharing options...
Christian F. Posted December 4, 2012 Share Posted December 4, 2012 (edited) This is such a bad, bad idea... Use MySQL's AUTO_INCREMENT for user IDs, and if you want a random and unique identifier for users use either PHP's uniqid () or MySQL's UUID() functions. Edited December 4, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271582-creating-a-random-string-of-16-characters-and-set-as-a-global-variable/#findComment-1397448 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.