When a user submits a form, some users submit some special characters, such as single quotation marks and double quotation marks. if you insert data directly to the database based on the normal string, the database may not be correctly inserted. For special characters in the form, php provides a function mysql_real_escape_string, which can escape special characters.
Mysql_real_escape_string ()Special characters in strings used in function escape SQL statements.
If yes, the function returns the escaped string. If it fails, false is returned.
SyntaxMysql_real_escape_string (string, connection)
| Parameters |
Description |
| String |
Required. Specifies the string to be escaped. |
| Connection |
Optional. MySQL connection is required. If not specified, use the previous connection. |
For example, we want to insert the string "my name's zhangsan" into the database. if you write an SQL statement as follows, an error is returned.
$str="my name's zhangsan";mysql_query("insert into table (field1) values ('".$str."')");
In this case, an error is reported. The single quotes in the string conflict with the SQL single quotes.
Which of the following statements is true?
// Escape the string to use $ str = mysql_real_escape_string ("my name's zhangsan"); mysql_query ("insert into table (field1) values ('". $ str. "')");
In this case, the string stored in the database is "my name/'s zhangsan". to tell the truth about this string, you need to use stripslashes () after reading the data () the function removes the backslash.