0

I guys, I have a public function user_exists to check if the username already exists on my database table.

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

}

And I want to check if the email exists, should I copy paste the user_exists function and just change the function name and the prepare statement like this?

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

}

Thanks!

Christopher Rioja
  • 149
  • 2
  • 2
  • 10

4 Answers4

1

You could make a private method that other methods use within the class:

<?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());
        }
    }

}
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

Combine it into one like this:

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

}

Now $variable is whatever you want to bind, and the $statement is the query statement you want to run. This way means less code to write/decode if theres an error. You just have to pass the information you want into the function

NoLiver92
  • 882
  • 3
  • 15
  • 39
0

I could not test this, but try this one:

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());
   }
}
TecBrat
  • 3,643
  • 3
  • 28
  • 45
  • Got the idea of the switch statement here: http://stackoverflow.com/questions/182287 – TecBrat Nov 23 '13 at 18:47
  • You asked about shortening the code, and I just noticed the if then else to return a boolean. You can just return the value of the comparison and save the whole if-then-else statement. (see line 24 in my code.) – TecBrat Nov 24 '13 at 12:59
0

How can I shorten that code?

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