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!

5
  • 6
    Why don't you try it? Commented Nov 23, 2013 at 18:09
  • 2
    or just check_exists($key, $value) use $key for the db column Commented Nov 23, 2013 at 18:13
  • I already tried and it is working fine but its a long code. How can I shorten that code? :/ Commented Nov 23, 2013 at 18:13
  • 2
    You should have posted/stated that --^ in your question then. Commented Nov 23, 2013 at 18:15
  • Don't be bashful about trial and error. The industry calls it "iterative testing". If you you are testing and had UPDATE or other write queries, then I'd say just make sure you are operating on test data or that you have a good backup (or both). Commented Nov 23, 2013 at 18:15

4 Answers 4

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

}
?>
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

2 Comments

Got the idea of the switch statement here: stackoverflow.com/questions/182287
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.)

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.