Are PHP PDO statements automatically escaped, or only prepared statements?
For example, assume that $username
and $password
are user inputs. Is the following code secure, or is it vulnerable to injection?
$dbh = new PDO("mysql:host=localhost;dbname=mydb", $my_mysql_username, $my_mysql_password);
$sth = $dbh->query("SELECT * FROM users WHERE username='$username' AND password='$password'");
$result = $sth->fetch();
if(!$result){
$dbh->exec("INSERT INTO users (username, password) VALUES ('$username', '$password')");
}
(The above code is purely hypothetical, for example purposes only.)
If they are not automatically escaped, does PDO provide any extra protection over the mysql_
functions in this situation?