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!
4 Answers 4
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());
}
}
}
?>
Comments
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
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
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());
}
}
check_exists($key, $value)use$keyfor the db columnUPDATEor 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).