Jump to content

Random number generator and databases


jonahpup

Recommended Posts

Hi there,

 

I have a random number generator on my page, which at random selects a 5 digit number... that part works... I also have it set to insert that random number into a database... that part also works...

 

What I want to know, is how do I tell it, to check the "rdm_num" field in the database for the number that has just been generated, and if it already exists, generate a new number and add only the new number to the database???

 

Does this make sense... if someone could help me, that would be awesome!

 

Cheers

Chris

Link to comment
https://forums.phpfreaks.com/topic/49606-random-number-generator-and-databases/
Share on other sites

Here is the basic concept. You may need to put the first part in a function so you can call it with each number generation.

 

<?php

$rand_num=rand(10000, 99999);

//possibly start function here
// function check_number(){

$query="SELECT rdm_num FROM table_name WHERE rdm_num = '$rand_num'";
$result=mysql_query($query);

while($row=mysql_fetch_object($result))
{
$matches[]=$row->rdm_num;
}

// }  //end function here so it can be called below


if(count($matches) > 0)
{
echo 'This number already exists, creating new number';
$new_rand_num=rand(10000,99999);
            // you may want to call the function here and check this new number as well.
           
}
else
{
echo 'This number does not exist yet';
}

?>

 

not sure if this could cause problems with a looping function call or not, but hopefully you get the idea behind this. A little difficult to give ya  the correct code without seeing what your code is for this to start with.

 

Hope this helps

Hey thanks for that chronister...

 

Im still having trouble (im new to php, so still trying to get the hang of what goes where)...

 

here is my code....

<?php

$rand_num=rand(10000, 99999);

//possibly start function here


$con = mysql_connect("localhost", "username", "password");

if (!$con)
{
die ('Could not connect: ' .mysql_error());
}

mysql_select_db ("dbaseName", $con);

function check_number(){

$query="SELECT voucherID FROM vouchers WHERE voucherID = '$rand_num'";
$result=mysql_query($query);

while($row=mysql_fetch_object($result))
{
$matches[]=$row->rdm_num;
}
}

// }  //end function here so it can be called below


if(count($matches) > 0)
{
echo 'This number already exists, creating new number';
$new_rand_num=rand(10000,99999);
    check_number();
       // you may want to call the function here and check this new number as well.
           
}
else
{

$sql = "INSERT INTO vouchers (voucherID) VALUES ('$rand_num')";

echo "$rand_num";

}

mysql_close($con);

?>

 

It will display a random number, but it doesnt put it in the database... hence im not even sure its checking database for the number in the first place...

 

oh... and just so you know im not a COMPLETE idiot... I do actually have the username and password etc in the script... just didnt want to post it for the world to see...

 

<?php
$rand_num=rand(10000, 99999);

//I prefer making one connectdb function and making it available to all pages in a site
// so anytime you need to initiate a connection, you can simply call this function
// just my opinion though
function connectdb()
{
$con=mysql_connect('localhost', 'username', 'password');
mysql_select_db('dbname') or die('Unable to select database!');

};

function check_number()
{
global $rand_num,$matches; // we have to make the variables global 
          connectdb(); // call to database function and initiate connection
$query="SELECT voucherID FROM vouchers WHERE voucherID = '$rand_num'";
$result=mysql_query($query);
	while($row=mysql_fetch_object($result))
	{
		$matches[]=$row->rdm_num;
	}
}
// you need to call the function here first so it checks it in the first place
check_number(); 
if(count($matches) > 0)
{
echo 'This number already exists, creating new number';
$rand_num=rand(10000,99999);
    check_number();
}
else
{
         connectdb(); 
          // technically, the connection is still active and we don't have to call it again, but I do anyway
$sql = "INSERT INTO vouchers (voucherID) VALUES ('$rand_num')";
$result=mysql_query($sql); // gotta actually run the query here
echo "$rand_num";

}

mysql_close($con);// I normally don't use this because PHP closes it when it is done with it
?>

 

Try that...

 

A couple things were wrong, both in what I gave you and what you had.

 

First we have to set the $matches, and $rand_num variables global so that the vars created outside the function are available within, and the one created inside it is available outside of it. See the php manual for variable scope. An alternative to making the $rand_num var global would be to pass it into the function like so check_number($rand_num)

 

Second, you had not called the function in the first place to check the number. It was skipping the whole function because it had not been initiated.

 

Third, you had not called the mysql_query() function to actually run the $sql variable.

 

We SHOULD properly terminate the connection properly when we are done with it, but some folks do and some don't. I happen to be in the latter group.

 

Hope this helps

 

Nate

thanks chronister... it works fine...

 

i don't see the purpose of this. it seems as if this would use a lot of unnecessary resources. why does it need to be a 5 digit number? why can't you just use an AUTO_INCRIMENTing 'id' column?

um... mainly because I want random numbers, not sequential...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.