(Edit:Guys, Before jumping to any conclusions, I'm asking how do you escape a query variable from the Example#2 from php.net website. I tried lot of ways but they all gave me errors. If you can please read that Example and post your version of that exact Example#2. Also please read about why they have that example there.)
I was searching for a reliable 'row:count' method to use with PHP PDO across multiple database types, and came across below code from php.net http://php.net/manual/en/pdostatement.rowcount.php (See Example:#2) It says to do a row count to see if an entry exists in a database using a SELECT statement, the error proof method is to use PDO::query() instead of PDOStatement::fetchColumn().
My question is I know how to bind and execute with PDO, but I don't know how to assign a user submitted variable($username) to this $sql statement and escape it successfully?
Is it possible to bind parameters to this $sql mehod using PDO?
            try{
            $conn = new PDO($dsn, $db_username, $db_password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            $this->db = $conn;
        } catch(PDOException $e){
            echo 'Error:'.$e;
        }
    public function usernameExists($username){
        //Check db for a match.
        $sql = "SELECT * FROM users WHERE username = '".$username."'";
        $results = $this->db->query($sql);
        if($results->fetchColumn() > 0){
            //Matching username found in the db
            return true;
        }else{
            //No matching username in db
            return false;
        }
}
 
     
     
    