-1

The following function does not return any value when if statement is true; otherwise it is returning $var1; it shows the correct value.

function setValue($var1) {
    global $con;
    global $k;
    $k++;
    $var1 = strtolower($var1);
    $var1 = preg_replace('/[^a-zA-Z0-9_-]/', '-', $var1);
    $var1 = preg_replace('/-+/', '-', $var1);
    $result1 = mysqli_query($con, "select * from tbl_name where name='".$var1."'");
    if (mysqli_num_rows($result1) > 0) {            
        setValue($var1.'-'.$k);
    }
    else {
        //die($var1);
        return $var1;
    }           
}
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Asif
  • 65
  • 8

2 Answers2

0

Edit: Sorry i didn't see it's recursive but seems like you are doing it wrong still. return and recursive like that is not a good idea. I think you should return values by this method and then use it in a loop by passing var and k parameters.

First of all you need to check your if statement. You are not returning any value if first condition is true, you are calling some method only.

if(mysqli_num_rows($result1)>0)
{           
    setValue($var1.'-'.$k);
    return($var1);
}
else
{
    "Give some error to user here";
     return($var1); //if you still want to see it...
}

It's better to inform user if "else" is true while you are checking DB works like that.

ReadyFreddy
  • 898
  • 8
  • 20
0

change:

setValue($var1.'-'.$k);

to:

return ($var1.'-'.$k);
Hossam
  • 1,126
  • 8
  • 19