PHP for anti-SQL injection

Source: Internet
Author: User

You need to prevent SQL injection when querying the database

Method of implementation:

PHP has its own method to escape SQL statements, such as database query statements need to be preceded by some characters with a backslash. These characters are single quotation marks ('), double quotation marks ("), backslashes (\), and NULL NUL (characters).

String addslashes ( string $str )

The function returns a string

Example

Example #1 a addslashes () Example

<?php
$str = "Is your name O‘reilly?";

// 输出: Is your name O\‘reilly?
echo addslashes($str);
?>
Thinkphp provides security protection automatically, thinkphp escape_string processing for string type data (real_escape_string,mysql_escape_string)to effectively prevent SQL injection problems, the official recommendation:
    • Query conditions use arrays as much as possible, which is a more secure approach;
    • If you have to use string query conditions, use a preprocessing mechanism;
    • Use automatic validation and auto-completion mechanisms for custom filtering of applications;
    • If the environment permits, use the PDO method as much as possible and use parameter bindings.

Query condition preprocessing

  This approach is similar to placing a placeholder in a query statement and passing in the parameter as an array

For example:

$Model ->where ( span class= "str" > "id=%d and Username= '%s ' and xx= '%f '" ,array ( $id , $username , $xx Span class= "pun"))->select ();

$Model->where("id=%d and username=‘%s‘ and xx=‘%f‘",$id,$username,$xx)->select();

PHP for anti-SQL injection

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.