1

Here is my code. Basically it generates a random string and I want to regenerate if the generated code exists in my db, but I always get recurring error.

What can I do to stop recurring error?

Here's my error

cannot redeclare exists_in_db (previously declared)

Code:

function exists_in_db($str)
{
    $check_badge_in_paids = "sql query";
    return mysqli_num_rows($check_badge_in_paids) > 0;
}

function rand_string()
{ 
     $str = ""; 

     do 
     { 
          $str = substr(md5(microtime()), 0, 5); 
     } 

     while(exists_in_db($str)); 

     return $str; 
} 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Crys
  • 41
  • 1
  • 9
  • cannot redeclare exists_in_db (previously declared) – Crys May 27 '18 at 22:34
  • I'm not sure what you're trying to achieve with the data you are querying. The value set by microtime is going to be different depending on time the script takes to run and previous query takes. As for the error, I'm not sure, but you do nothing with the `$str` param you pass in to the `exists_in_db()` function. I just tested your code a bit by the `do` incrementing a value and `exists_in_db()` returns true when less than certain value. worked fine. Do you include this file or the function more than once? Can you show the full error in its entirety from your logs? – James May 27 '18 at 22:52
  • i wanted to assign unique id for each user.so if the generated string already assigned to a user ..i want to generate new string – Crys May 27 '18 at 23:04
  • https://stackoverflow.com/questions/1953857/fatal-error-cannot-redeclare-function/19754317 - take a look at this. – Juakali92 May 27 '18 at 23:11
  • Possible duplicate of ["Fatal error: Cannot redeclare "](https://stackoverflow.com/questions/1953857/fatal-error-cannot-redeclare-function) – Nick May 28 '18 at 02:59

1 Answers1

0

Well Crys that error would only occur if you've previously declared that function, so here's what I advice you do, clear this code and do this:

var_dump(function_exists('exists_in_db'));

OR: at the top where you have declared the function, do this:

var_dump(function_exists('exists_in_db'));
die;

if it returns true, then you have to check you code for previous declarations, probably included files etc.

samezedi
  • 621
  • 5
  • 15
  • is this is a solution though? feel like this could just be a comment – Juakali92 May 27 '18 at 22:54
  • If it helps solve the problem, then it is a solution – samezedi May 27 '18 at 22:56
  • 3
    advising them to debug and updating the question with the dumped result is helpful information to work towards a solution. Asking them to debug isn't an answer – Juakali92 May 27 '18 at 23:02
  • as you can see i'm doing do while..does it cause the error? – Crys May 27 '18 at 23:05
  • Just adding to what Juakali92 said (in a friendly way), stating "*If it helps solve the problem*" is not a certainty, making it a guess or shot in the dark because you said "IF" and not "I know this is the issue, this fixes it" etc. Educated guess perhaps, maybe even correct, but at this point we can't know for certain :) just fyi. – James May 27 '18 at 23:19