0

Im trying the login with twitter functionality.

if twitter returned username already exist in my database I am creating a new username for that user.

following is my code

class myclass{
    function getusername($username){
        global $db;
        $sql ="select * from us_signup where username ='".$username."'";
        echo $sql;
        $results = $db->rawQuery($sql);     

        if(!empty($results)){
        echo 'if';
        print_r($username);
            $username = $username.rand(0,1000);
            $this->getusername($username);
        }else{
        echo "else";
        print_r($username);
            return $username;
        }
    }

    function insertintodb(){
        echo "oldusername : ".$username;
        $username = $this->getusername($username);
        echo "newusername : ". $username;   
    }

}

1) insertintodb calls getusername with currentusername got from twitter

2) getusername checks it in the database

3) if the username exist in the database then it adds a random number to it and then calls itself

4) if the username doesn't exist in the database it returns the username

5) getusername calls itself until it finds username which doesn't exist in the database

My problem is

I am calling the function getusername with 'sugumar' which exist in the database so it calls itself again with newusername 'sugumar1234'(1234 is a random number can be anynumber). 'sugumar1234' doesn't exist in the database so I expect it return 'sugumar1234' but it returns nothing

following the ouput of print

oldusername : sugumarselect * from us_signup where username ='sugumar'ifsugumarselect * from us_signup where username ='1234'else1234newusername :

1 Answer 1

3

If I understand correctly, a return statement seems to be missing in the if part of your code.

$this->getusername('1234');

Should be

return $this->getusername('1234');
Sign up to request clarification or add additional context in comments.

2 Comments

if you don't mind could you please explain why should I need return... because I really don't understand....
When you call getUserName with sugumar parameter, this name exists in the database, therefore, the function getUserName calls itself with a new (randomized) parameter, this time the new paremeter (let's say foo) does not exist in the database, so it's returned by return $username; statement. However, this returned value is not affected to any variable nor returned in the first getUserName call (the one with sugumar parameter), therefore it's lost and the function does not return anything. Not sure if I'm clear, if not, I may make a scheme.

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.