Check username exist in database CI/php

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Post Reply
User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 290
Joined: Sat Mar 23, 2013 8:24 am

Re: Check username exist in database CI/php

Post by Strider64 » Wed Aug 07, 2013 12:48 pm

This might help??? This is how I check to see if a username already exits in a database (I'm using PDO):

Code: Select all

    // Method checks to see if username isn't already taken 
    //and returns true if it is already taken:
    public function isUsernameAvailable() {

        // Connect to PDO database:
        $db = Database::getInstance();
        $pdo = $db->getConnection();

        $query = "
            SELECT
                1
            FROM users
            WHERE
                username = :username1
        ";

        $query_params = array(
            ':username1' => $this->username
        );

        // These two statements run the query against your database table.
        $stmt = $pdo->prepare($query);
        $result = $stmt->execute($query_params);

        // The fetch() method returns an array representing the "next" row from
        // the selected results, or false if there are no more rows to fetch.	   	   
        return $row = $stmt->fetch();
        // If a row was returned, then we know a matching username was found in
        // the database already and we should return a boolean value back.	   
    }

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Check username exist in database CI/php

Post by johnj » Fri Aug 09, 2013 9:05 pm

1).What do you mean by "not working"?

2).you should post this to the codeigniter forum

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests