gamerx Posted August 14, 2010 Share Posted August 14, 2010 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: http://paste.tgxn.net/?s=9 Link to comment https://forums.phpfreaks.com/topic/210715-checking-if-data-exists-loop/ Share on other sites More sharing options...
wildteen88 Posted August 14, 2010 Share Posted August 14, 2010 Take a look at this thread http://www.phpfreaks.com/forums/index.php/topic,307316.0.html It is a similar question to yours. You should be able to modify the function I posted to your needs. Link to comment https://forums.phpfreaks.com/topic/210715-checking-if-data-exists-loop/#findComment-1099195 Share on other sites More sharing options...
gamerx Posted August 14, 2010 Author Share Posted August 14, 2010 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 https://forums.phpfreaks.com/topic/210715-checking-if-data-exists-loop/#findComment-1099226 Share on other sites More sharing options...
wildteen88 Posted August 14, 2010 Share Posted August 14, 2010 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 https://forums.phpfreaks.com/topic/210715-checking-if-data-exists-loop/#findComment-1099231 Share on other sites More sharing options...
gamerx Posted August 14, 2010 Author Share Posted August 14, 2010 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 https://forums.phpfreaks.com/topic/210715-checking-if-data-exists-loop/#findComment-1099270 Share on other sites More sharing options...
wildteen88 Posted August 14, 2010 Share Posted August 14, 2010 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 https://forums.phpfreaks.com/topic/210715-checking-if-data-exists-loop/#findComment-1099271 Share on other sites More sharing options...
gamerx Posted August 14, 2010 Author Share Posted August 14, 2010 that seems to work, but i have no way of testing if it created a unique code Thanks Link to comment https://forums.phpfreaks.com/topic/210715-checking-if-data-exists-loop/#findComment-1099273 Share on other sites More sharing options...
wildteen88 Posted August 14, 2010 Share Posted August 14, 2010 echo $content or insert $content into your database. Link to comment https://forums.phpfreaks.com/topic/210715-checking-if-data-exists-loop/#findComment-1099276 Share on other sites More sharing options...
gamerx Posted August 14, 2010 Author Share Posted August 14, 2010 thankyou Link to comment https://forums.phpfreaks.com/topic/210715-checking-if-data-exists-loop/#findComment-1099278 Share on other sites More sharing options...
gamerx Posted August 14, 2010 Author Share Posted August 14, 2010 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: Link to comment https://forums.phpfreaks.com/topic/210715-checking-if-data-exists-loop/#findComment-1099290 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.