Define and use special characters in strings used in the mysql_real_escape_string () function to escape SQL statements. The following characters are affected: if x00nr & quot; x1a is successful, the function returns the escaped string. If it fails, false is returned. Syntax mysql_real_escape_string (string, connection)
Definition and usage
The mysql_real_escape_string () function escapes special characters in strings used in SQL statements.
The following characters are affected:
\ X00
\ N
\ R
\
'
"
\ X1a
If yes, the function returns the escaped string. If it fails, false is returned.
Syntax
mysql_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. |
Description
This function escapes special characters in string and considers the connected current character set. Therefore, it can be safely used for mysql_query ().
Tips and comments
Tip: you can use this function to prevent database attacks.
Example
Example 1
Example 2
Database attacks. This example shows what will happen if we do not apply the mysql_real_escape_string () function to the user name and password:
The SQL query will be like this:
SELECT * FROM users WHERE user = 'John' AND password = ''OR'' =''
This means that any user can log on without entering a valid password.
Example 3
The correct method to prevent database attacks: