1

After following some answers and articles finally i came up with the function that will generate key(number) automatically when it is not exist in the database and codes works, but the problem is when the code exist the notification that "CODE EXIST" form something like a loop and print multiple notifications. Base on my codes where do i get it wrong and how can I fix it?

    <?php
 //HERE IS THE FUNCTION  
      function MyFunction($xIsConnection){
    // CODE GENERATION
      //$Code=(rand(10,1000));
       $Code='001';
       $query= "SELECT * FROM parent WHERE code='$Code'";
        if($result= mysqli_query($xIsConnection,$query)){
      if(mysqli_num_rows($result)>0){
       echo " CODE EXIST<br>";

  // CALL FUNCTION TO GENERATE NEW CODE
         MyFunction($xIsConnection);
            }
       else{
       echo "NOT EXIST  <br>";
       echo $Code;
         }
        }
    else{
    echo"failed";
    }
    }
    require_once('dbConnect.php');
    MyFunction($con);
    mysqli_close($con);

    ?>
6
  • Did you setup a connection($con)? Commented Feb 24, 2017 at 10:10
  • yes i did @ WasteD, as @Mr. Laststringx said below i see that is the problem but i don't have how can i get out of it? any idea with code example. Commented Feb 24, 2017 at 10:23
  • Where does this funtion generate code? It just sets $code but doesnt do something with it. Commented Feb 24, 2017 at 10:39
  • below the comment " CODE GENERATION" there is the commented line "$Code=(rand(10,1000));" that is the one generate i use set $code so that i can test the result with the existing value in the database@ WasteD Commented Feb 24, 2017 at 10:58
  • Alright you check if a code in the database exist. And then you generate a new one? What is the reason for that? Commented Feb 24, 2017 at 11:43

1 Answer 1

1

The answer is never ending recursion.

<?php
     //HERE IS THE FUNCTION  
          function MyFunction($xIsConnection){
        // CODE GENERATION
          //$Code=(rand(10,1000));
           $Code='001';
           $query= "SELECT * FROM parent WHERE code='$Code'";
            if($result= mysqli_query($xIsConnection,$query)){
          if(mysqli_num_rows($result)>0){
           echo " CODE EXIST<br>";

      // CALL FUNCTION TO GENERATE NEW CODE
             MyFunction($xIsConnection);         // this line is responsible for your error. Recursion
                }
           else{
           echo "NOT EXIST  <br>";
           echo $Code;
             }
            }
        else{
        echo"failed";
        }
        }
        require_once('dbConnect.php');
        MyFunction($con);
        mysqli_close($con);

        ?>
Sign up to request clarification or add additional context in comments.

5 Comments

so how can i control it because that is the function to auto generate code if the previous generated one exist in database? is there any way to achieves it? @Mr. Laststringx
Use aniother function to check if it exist in database. Store the result in a boolean variable and then check its value. @f2k
if am not out of what you have said i created this "<?php function MyFunction($xIsConnection){ $Code=(rand(10,1000)); //$Code='001'; $query= "SELECT * FROM parent WHERE code='$Code'"; if($result= mysqli_query($xIsConnection,$query)){ if(mysqli_num_rows($result)>0){ return true; } else{ return false; echo $Code; } } else{ echo"failed"; } } require_once('dbConnect.php'); $MyFunctionResult=MyFunction($con); if($MyFunctionResult== true) { echo "exist"; } else{ echo "not exist"; } mysqli_close($con); ?>" but how can i check its value ? @ Mr. Laststringx
i checked if code exist and return true, but how can i check values since it seems to be i will fall under the use of the same problem for reuse the same function @ Mr. Laststringx
Either • export all the codes in a set from the database and then generate a code which is not there in the set. • or generate a set of codes and then check for it's existence in the database. (This can cost you some time)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.