I am trying to make a PHP function check against a MySQL DB if a value that has been randomly created previously exists, if it does then for it to repeat until it makes a value in which does not exist.
Currently i have failed miserably, although in precedence i believed my method would have worked, it just does not.
The PHP
I tried this in the script on its own and then realised that php functions do not run unless they are called.
conn(); //db connection
function genID() {
$newIDref = uuaig(10);
$queryCHECK = "SELECT *
FROM contentArticle_vids
WHERE videoArticle_id = '$newIDref'
";
$resultCHECK = mysql_query($queryCHECK);
$numrows = mysql_num_rows($resultCHECK);
if ($numrows !== 0) {
genID();
die();
}
}
So.. i then tried this, of which then made it really unhappy (broke the whole page).
conn(); //db connection
genID();
function genID() {
$newIDref = uuaig(10);
$queryCHECK = "SELECT *
FROM contentArticle_vids
WHERE videoArticle_id = '$newIDref'
";
$resultCHECK = mysql_query($queryCHECK);
$numrows = mysql_num_rows($resultCHECK);
if ($numrows !== 0) {
genID();
die();
}
if ($numrows === 0) {
return $newIDref;
die();
}
}
For reference the random string generator is the uuaig(10).
I ultimately need the random string generated and then returned if the string is confirmed as non existent in the database.
How do i proceed?
die()there? for debugging or is the actual code? if you want to recursively call your function, you must end it withreturn genID();please notice that your code is missingreturn, after that, bothdie()are unnecesaryuuaig()is te random string gen yes.