Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8881537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:22:03+00:00 2026-06-14T20:22:03+00:00

Are PHP PDO statements automatically escaped, or only prepared statements? For example, assume that

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:22:04+00:00Added an answer on June 14, 2026 at 8:22 pm

    Only prepared statements provide automagic escaping, assuming you don’t have some ugliness like magic quotes enabled. And only the data in the params is escaped, not anything that’s already in the SQL string when you prepare the statement.

    If you want the benefits of auto escaping, you’ll have to prepare a statement and feed it the data separately.

    $sth = $dbh->prepare("SELECT * FROM users WHERE username=? AND password=?");
    $sth->execute(array($username, $password));
    

    Otherwise, you get little to no protection over mysqli_query and friends. (I refuse to even mention mysql_query, because no self-respecting PHP programmer uses it anymore. Oh, wait..damn. Well, that’s the only mention it gets here.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://php.net/manual/en/pdo.prepared-statements.php If an application exclusively uses prepared statements, the developer can be sure that
Are PHP/mysql prepared statements possible when mysqli and PDO are not available? Are there
PHP PDO Singleton Class: <?php require_once('app/config.php'); // Require constants HOST, DATABASE, USER, PASSWORD /*
I have been spotting the sentence PHP PDO's prepared statements prevents SQL injection .
I have a web application using PHP and PDO with SQLSRV prepared statements to
I'm new to PHP and PDO, and I try to use prepared statements here.
I'm playing around with prepared statements in PHP/PDO. The basic queries work fine, passing
Using PDO with prepared statements (I learned from here: http://www.kitebird.com/articles/php-pdo.html ) I'm using a
Using PHP v. 5.2.14 and PDO-MySQL extension. I am new to prepared statements. Need
Trying to make insert or update sql using the prepared statements from php's pdo.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.