Can the following code effectively prevent SQL injection?

Source: Internet
Author: User
Can the following code effectively prevent SQL injection? In general, how does the code below {code...} effectively prevent SQL injection?

How do you do this.


  SetAttribute (PDO: ATTR_EMULATE_PREPARES, false); // disable the simulation of prepared statements $ dbh-> exec ("set names 'utf8 '"); $ SQL = "select * from table where username =? And password =? "; $ Query = $ dbh-> prepare ($ SQL); $ exeres = $ query-> execute (array ($ username, $ pass); if ($ exeres) {while ($ row = $ query-> fetch (PDO: FETCH_ASSOC) {print_r ($ row) ;}}$ dbh = null;?>

Reply content:

Can the following code effectively prevent SQL injection?

How do you do this.


  SetAttribute (PDO: ATTR_EMULATE_PREPARES, false); // disable the simulation of prepared statements $ dbh-> exec ("set names 'utf8 '"); $ SQL = "select * from table where username =? And password =? "; $ Query = $ dbh-> prepare ($ SQL); $ exeres = $ query-> execute (array ($ username, $ pass); if ($ exeres) {while ($ row = $ query-> fetch (PDO: FETCH_ASSOC) {print_r ($ row) ;}}$ dbh = null;?>

We recommend that you write this statement to prevent injection attacks more effectively.

......$sql="select * from table where username = ?";......    while ($row = $query->fetch(PDO::FETCH_ASSOC) && $row['pass'] == $pass) {        print_r($row);    }

Your code can completely prevent SQL injection, because PDO is SQL preprocessing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.