fireinspace
fireinspace

Reputation: 197

PHP: Run function and return values

I am trying to run a function that checks a db table for a username and an email. I call the function with two parameters ($user_username, $user_password). The function checks the database to see if those values exist. However, I cannot get the variables from the function to return properly. Here's what i have so far:

Function:

class registerClass{
        public function checkUser($user_username, $user_email){
        //connect to db via pdo...

        $st_1 = $handler->prepare("SELECT * FROM tbl_users WHERE user_username = '$user_username'");
        $st_1->execute();
        if($st_1->rowCount() > 0){$user_exists = '1';}

        $st_2 = $handler->prepare("SELECT * FROM tbl_users WHERE user_email = '$user_email'");
        $st_2->execute();   
        if($st_2->rowCount() > 0){$email_exists = '1';}

    }
}

Call to function:

$object = new registerClass();
$object->checkUser($user_username, $user_email);

if($user_exists >= '1'){$errors[] = "Username taken";}
if($email_exists >= '1'){$errors[] = "Email taken";}

For some reason the errors never get thrown. I'm not sure what I'm doing wrong here.

Upvotes: 0

Views: 66

Answers (3)

gafreax
gafreax

Reputation: 410

When you call a class method like: $object->checkUser($user_username, $user_email); or a function like : checkUser($user_username, $user_email); you must remember that php pass the params by copy, not by reference.

So by default what you pass (for example $user_name), it's not the same $user_name on function/method body. Seee http://php.net/manual/en/functions.arguments.php for more.

You can resolve your problem by using new method declaration this code:

    public function checkUser(&$user_username, &$user_email){
      //connect to db via pdo...

      $st_1 = $handler->prepare("SELECT * FROM tbl_users WHERE user_username = '$user_username'");
      $st_1->execute();
      if($st_1->rowCount() > 0){$user_exists = '1';}

      $st_2 = $handler->prepare("SELECT * FROM tbl_users WHERE user_email = '$user_email'");
      $st_2->execute();   
      if($st_2->rowCount() > 0){$email_exists = '1';}

}

So to have have an argument to a function passed by reference, prepend an ampersand (&):checkUser(&$user_username, &$user_email)

Upvotes: 0

Barmar
Barmar

Reputation: 781004

The variables you're setting are local to the function, they're not visible in the scope of the caller. Instead, the function should return the variables in an array:

class registerClass{
    public function checkUser($user_username, $user_email){
        $user_exists = $email_exists = false;
        //connect to db via pdo...

        $st_1 = $handler->prepare("SELECT * FROM tbl_users WHERE user_username = '$user_username'");
        $st_1->execute();
        if($st_1->rowCount() > 0){$user_exists = true;}

        $st_2 = $handler->prepare("SELECT * FROM tbl_users WHERE user_email = '$user_email'");
        $st_2->execute();   
        if($st_2->rowCount() > 0){$email_exists = true;}

        return array($user_exists, $email_exists)
    }
}

You can then use it like this:

list($user_exists, $email_exists) = $object->checkUser($user_username, $user_email);
if($user_exists){$errors[] = "Username taken";}
if($email_exists){$errors[] = "Email taken";}

I've also changed the values from strings with 0 and 1 to booleans false/true.

Upvotes: 1

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

You should use return to return value by function and than in place where you call your function use for example list to get function return values.

Complete code:

<?php


class registerClass
{
    public function checkUser($user_username, $user_email)
    {
        //connect to db via pdo...

        $user_exists = 0;

        $email_exists = 0;


        $st_1 = $handler->prepare(
            "SELECT * FROM tbl_users WHERE user_username = '$user_username'"
        );
        $st_1->execute();
        if ($st_1->rowCount() > 0) {
            $user_exists = '1';
        }

        $st_2 = $handler->prepare(
            "SELECT * FROM tbl_users WHERE user_email = '$user_email'"
        );
        $st_2->execute();
        if ($st_2->rowCount() > 0) {
            $email_exists = '1';
        }

        return array($user_exists, $email_exists);

    }
}


$object = new registerClass();
list($user_exists, $email_exists) = $object->checkUser(
    $user_username,
    $user_email
);

if ($user_exists >= '1') {
    $errors[] = "Username taken";
}
if ($email_exists >= '1') {
    $errors[] = "Email taken";
}

However normally you rather don't set value 1 in that case but boolean true so you should rather use this code:

class registerClass
{
    public function checkUser($user_username, $user_email)
    {
        //connect to db via pdo...

        $user_exists = false;

        $email_exists = false;


        $st_1 = $handler->prepare(
            "SELECT * FROM tbl_users WHERE user_username = '$user_username'"
        );
        $st_1->execute();
        if ($st_1->rowCount() > 0) {
            $user_exists = true;
        }

        $st_2 = $handler->prepare(
            "SELECT * FROM tbl_users WHERE user_email = '$user_email'"
        );
        $st_2->execute();
        if ($st_2->rowCount() > 0) {
            $email_exists = true;
        }

        return array($user_exists, $email_exists);

    }
}


$object = new registerClass();
list($user_exists, $email_exists) = $object->checkUser(
    $user_username,
    $user_email
);

if ($user_exists) {
    $errors[] = "Username taken";
}
if ($email_exists) {
    $errors[] = "Email taken";
}

Upvotes: 1

Related Questions