Why the custom function is not working?

function checkDuplicateName($table, $column_name, $value, $db){
        $sqlQuery = "SELECT * FROM ".$table." WHERE ".$column_name." = :$column_name";
        $statement = $db->prepare($sqlQuery);
        $statement->execute(array(':column_name' => $value));

        if($row = $statement->fetch()){
            return true;
        return false;
    } catch(PDOException $e){

 if(checkDuplicateName('users', 'email', $username, $db)){  //$db is the database connection name
        $result = flashMessage("Username is already taken");

    else if(checkDuplicateName('users', 'email', $email, $db)){
        $result = flashMessage("Email is already taken");

This function checks if the username and email is already taken or not. The problem is when I try to submit the form, it shows me this error instead of pre-defined flashmessage() function.
An error occured: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘demo@yahoo.com’ for key ‘email’

You have a problem with the line below:

Your parameter name should be :column_name not :$column_name, otherwise PHP will replace it with the value of the variable $column_name.

1 Like

Solved, thanks mate, how I damn missed it… :blush:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.