PHP:运行函数和返回值

时间:2014-09-08 22:10:38

标签: php function pdo

我正在尝试运行一个函数来检查db表中的用户名和电子邮件。我用两个参数调用该函数($ user_username,$ user_password)。该函数检查数据库以查看是否存在这些值。但是,我无法从函数中获取变量以正确返回。这是我到目前为止所拥有的:

功能:

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';}

    }
}

致电功能:

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

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

出于某种原因,错误永远不会被抛出。我不确定我在这里做错了什么。

3 个答案:

答案 0 :(得分:1)

您应该使用return按功能返回值,而不是在您调用函数的位置返回值,例如list来获取函数返回值。

完整代码:

<?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";
}

但是通常你宁愿在这种情况下不设置值1但是布尔true所以你应该使用这个代码:

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";
}

答案 1 :(得分:1)

您设置的变量是函数的本地变量,它们在调用者的范围内不可见。相反,该函数应该返回数组中的变量:

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)
    }
}

然后您可以像这样使用它:

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

我还将01的字符串值更改为布尔值false / true

答案 2 :(得分:0)

当您调用类方法:$object->checkUser($user_username, $user_email);或类似函数checkUser($user_username, $user_email);时,您必须记住php通过副本传递params,而不是通过引用传递。

因此,默认情况下您传递的内容(例如$ user_name),它与函数/方法体上的$ user_name不同。见http://php.net/manual/en/functions.arguments.php了解更多信息。

您可以使用此方法的新方法声明来解决您的问题:

    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';}

}

因此要对通过引用传递的函数有一个参数,请添加&符号(&amp;):checkUser(&$user_username, &$user_email)