Jump to content

Checking if data exists [loop]


gamerx

Recommended Posts

 

 

Hi,

 

Im trying to generate a random 4- digit code, and then check if it is allready in the database.

 

My current code is below, but im not sure how to make it loop again, if the code exists.

 

Also, im not sure if this is the best way of doing this, maybe someone could suggest something better...

 

 

This is basicly what im trying to do:

1. hash the code

2. check if it exits in the DB

3. if it does, re-generate, and repeat 2., if not, return it

 

 

 

 

function getUniqueHash() {
global $mysql_server, $mysql_user, $mysql_password;

$code = substr(md5(uniqid(rand(), true)), 0, 4);

mysql_connect($mysql_server, $mysql_user, $mysql_password); // connect //
$sql = "SELECT * FROM `$mysql_db`.`short` WHERE `short` = '$code'";
$result = mysql_query($sql);
$results = mysql_numrows($result);
if($results > 0) {
	//GO BACK AND RE-GENERATE CODE AND REPEAT CHECK
} else {
	return $code;
}
mysql_close();
}

 

PasteBin:

Link to comment
Share on other sites

So, this?

 

function getUniqueHash() {
global $mysql_server, $mysql_user, $mysql_password;
$code = substr(md5(uniqid(rand(), true)), 0, 4);
mysql_connect($mysql_server, $mysql_user, $mysql_password); // connect //
$sql = "SELECT * FROM `$mysql_db`.`short` WHERE `short` = '$code'";
$result = mysql_query($sql);
$results = mysql_numrows($result);
mysql_close();
if($results > 0) {
	return getUniqueHash();
} else {
	return $code;
}
}

Link to comment
Share on other sites

I wouldn't query mysql for each unique code you generate as this could cause a lot of server load. As you never know if the function will be called once, five times or even a hundred times (depending on how may codes you have).

 

This is why I suggested in the other post to grab all the current codes within the database and add them to an array. I'd then use this array for checking to see if the current code exists. Like so

// a function for getting all codes
function getCodes()
{
    $query = 'SELECT short FROM short';
    $result = mysql_query($query);
    
    $codes = array();
    while(list($code) = mysql_fetch_row($result))
        $codes[] = $code;
    return $codes;
}

// grab the codes array
$codes = getCodes();

// this generates the unique code.
function getUniqueHash(&$codes)
{
$code = substr(md5(uniqid(rand(), true)), 0, 4);
return ((in_array($code, $codes) ? getUniqueHash($codes) : $code );
}

// grab the unique code
$unquie_code = getUniqueHash($codes);

Link to comment
Share on other sites

Am i doing it right?

 

i have this in my "functions"

 

// a function for getting all codes
function getCodes() {
global $mysql_server, $mysql_user, $mysql_password, $mysql_db;
mysql_connect($mysql_server, $mysql_user, $mysql_password); // connect //
$sql = "SELECT * FROM `$mysql_db`.`short`";
$result = mysql_query($sql);
$codes = array();
while(list($code) = mysql_fetch_row($result))
	$codes[] = $code;
mysql_close();
return $codes;
}

// this generates the unique code.
function getUniqueHash($codes) {
$code = substr(md5(uniqid(rand(), true)), 0, 4);
return (in_array($code, $codes) ? getUniqueHash($codes) : $code );
}

 

 

And this when i need to get a (unique) code:

 

// grab the codes array
$codes = getCodes();
// grab the unique code
$content = getUniqueHash($codes);

 

Also, thank you very much for all the help, its really good and i appreciate it

Link to comment
Share on other sites

Your query is wrong.

	$sql = "SELECT * FROM `$mysql_db`.`short`";

You should only select the field named short. Not all fields otherwise the while loop wont work.

	$sql = "SELECT `short` FROM `$mysql_db`.`short`";

 

With the above fix, you should test it to see if it works.

Link to comment
Share on other sites

Cant seem to find the edit button, another quick question:

 

how can i edit this to check if $long_url exists in the database, under `$mysql_db`.`long`.

 

so, $long_url exists in `$mysql_db`.`long`, then return the value in `$mysql_db`.`short` for it.

and also still checking for the short code

 

this is what my database looks like:

ScreenShot001.png

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.