1. Generate a Random Number
2. Check in the mysql database if the Random number was previously generated
3. If it was generated previously, go back to step 1.
4. If it is not found in the database, it was not generated before, so proceed and use the generated number.
And here is the code I used to achieve it:
Code: Select all
function generateRandom($input){
$random_number = intval( "0" . rand(1,9) . rand(0,9) . rand(0,9) . rand(0,9) . rand(0,9) ); // random(ish) 5 digit int
$random_string = chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)) . chr(rand(65,90)); // random(ish) 5 character string
$randomSum = $random_number.$random_string.'adjafhdklf';
$mostRandom = md5($randomSum.$input);
return $mostRandom;
};
//generate a random number
function generate_a_random(){
$a_random_string = generateRandom(generateRandom(generateRandom(generateRandom(2071990))));
return $a_random_string;
};
generate_a_random();
//check in DB
$checkForTwins = @mysql_query("select * from ad_details where unique_ad_id like '$a_random_string';");
if(!$checkForTwins){
echo 'checkForTwins failed';
echo mysql_error();
};
$result_of_twincheck = mysql_fetch_array($checkForTwins);
//check for twins
function check_for_twins(){
if($result_of_twincheck['unique_ad_id'] == $a_random_string){
generate_a_random();
check_for_twins();
}else{
$unique_ad_id = $a_random_string;
};
};
check_for_twins();
function insert_data(){
};
It seems there is a mistake in my code. I am getting an error that says 'Connection to the server was reset' when I test it in my local server in IE, and Firefox says that the server is too busy. Can someone please correct my code? I know there is a small mistake, but I just can't find it
Thanks is advance for everyone who helps...