使用功能 - Php

时间:2013-11-23 18:08:08

标签: php

我们,我有一个公共函数user_exists来检查我的数据库表中是否已经存在用户名。

public function user_exists($username) {

$query = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `username`= ?");
$query->bindValue(1, $username);

try{

    $query->execute();
    $rows = $query->fetchColumn();

    if($rows == 1){
        return true;
    }else{
        return false;
    }

} catch (PDOException $e){
    die($e->getMessage());
}

}

我想检查电子邮件是否存在,是否应该复制粘贴user_exists函数并只更改函数名称和准备语句?

public function email_exists($email) {

$query = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `email`= ?");
$query->bindValue(1, $email);

try{

    $query->execute();
    $rows = $query->fetchColumn();

    if($rows == 1){
        return true;
    }else{
        return false;
    }

} catch (PDOException $e){
    die($e->getMessage());
}

}

谢谢!

4 个答案:

答案 0 :(得分:1)

您可以创建一个其他方法在类中使用的私有方法:

<?php 
/**
 * Presume your user class yada...
 *
 */
class user{
    /**
     * Check email exists
     *
     * @param string $value
     * @return bool
     */
    public function email_exists($value){
        return $this->db_check_exists('email', $value);
    }
    /**
     * Check user exists
     *
     * @param string $value
     * @return bool
     */
    public function user_exists($value){
        return $this->db_check_exists('user', $value);
    }

    /**
     * Private method used by other check methods
     *
     * @param string $column
     * @param string $value
     * @return bool
     */
    private function db_check_exists($column, $value) {
        $query = $this->db->prepare("SELECT 1 FROM `users` WHERE `{$column}` = :value");
        $query->bindValue(':value', $value);
        try{
            $query->execute();
            $rows = $query->fetchColumn();
            if($rows == 1){
                return true;
            }else{
                return false;
            }
        } catch (PDOException $e){
            die($e->getMessage());
        }
    }

}
?>

答案 1 :(得分:0)

将它合并为一个:

public function exists($variable, $statement) {

$query = $this->db->prepare($statement);
$query->bindValue(1, $variable);

try{

$query->execute();
$rows = $query->fetchColumn();

if($rows == 1){
    return true;
}else{
    return false;
}

} catch (PDOException $e){
die($e->getMessage());
}

}

现在$ variable是你想绑定的,$ statement是你想要运行的查询语句。如果出现错误,这种方式意味着编写/解码的代码更少。您只需将所需信息传递给函数

即可

答案 2 :(得分:0)

我无法测试这个,但试试这个:

public function element_exists($element_name,$element_value) {

switch($element_name)
{
    case 'users':
        $safe_element = 'users';
        break;
    case 'email':
        $safe_element = 'email';
        break;
    default:
        return false;
}

$query = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `$safe_element`= ?");

$query->bindValue(1, $element_value);

try{

    $query->execute();
    $rows = $query->fetchColumn();

    return $rows == 1;

   } 
catch (PDOException $e){
    die($e->getMessage());
   }
}

答案 3 :(得分:0)

  

如何缩短代码?

public function email_exists($email) {
    $stmt = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `email`= ?");
    $stmt->bindValue(1, $email);
    return ( $stmt->execute()->fetchColumn() == 1 );
}