PDO protection against SQL Injection

Source: Internet
Author: User

PDO protection against SQL Injection

<Span style = "font-size: 18px;"> <? Php $ dbh = new PDO ("mysql: host = localhost; dbname = demo", "user", "pass"); $ dbh-> setAttribute (PDO: ATTR_EMULATE_PREPARES, false); // disable the simulation effect of prepared statements $ dbh-> exec ("set names 'utf8'"); $ SQL = "select * from test where name =? And password =? "; // You can also use the format $ stmt = $ dbh-> prepare ($ SQL); $ exeres = $ stmt-> execute (array ($ testname, $ pass); if ($ exeres) {while ($ row = $ stmt-> fetch (PDO: FETCH_ASSOC) {print_r ($ row );}} $ dbh = null; // a few more words: & nbsp; When prepare () is called, the query statement has been sent to the database server, and there is only a placeholder? The data submitted by the user is not sent in the past. When the call is to execute (), the value submitted by the user is sent to the database. They are sent separately, and the two are independent, SQL attackers have no chance. But we need to pay attention to the following situations. PDO does not help you prevent SQL injection. 1. You cannot make placeholders? Replace a set of values, such as: SELECT * FROM blog WHERE userid IN (? ); 2. You cannot replace a placeholder with a data table name or column name, for example, SELECT * FROM blog order ?; 3. Can't you make placeholders? Replace any other SQL syntax, such as: SELECT EXTRACT (? FROM datetime_column) AS variable_datetime_element FROM blog; </span>

 

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.