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