PHPmysql_real_escape_string NOTE _ PHP Tutorial

Source: Internet
Author: User
Note one of PHPmysql_real_escape_string. The mysql_real_escape_string () function escapes special characters in strings used in SQL statements. The following characters are affected: if x00nrx1a succeeds, the function returns the special characters in the escape character mysql_real_escape_string () to escape the strings used in the SQL statement.

The following characters are affected:

  • X00
  • N
  • R
  • '
  • "
  • X1a

If yes, the function returns the escaped string. If it fails, false is returned.

Use mysql_real_escape_string (string, connection)

  • The string parameter, required. Specifies the string to be escaped.
  • The connection parameter. optional. MySQL connection is required. If not specified, use the previous connection.

This function escapes special characters in string and considers the connected current character set. Therefore, it can be safely used for mysql_query (). This function is used to prevent database attacks.

Simple use of mysql_real_escape_string ()
 
SQL injection (mysql_real_escape_string () is not used ())

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 usersWHERE user='john' AND password='' OR ''=''

This means that any user can log on without entering a valid password.

The correct method to prevent database attacks:
 
Some experiences

Mysql_escape_string () also achieves similar results. For these two functions, remember that when I use the mysql_real_escape_string () function, the program always fails and I don't know the cause of the error. then I changed it to the mysql_escape_string () function. Find the manual and find that when mysql_real_escape_string () is used, you must first establish a database connection; otherwise, the error-___-|

Mysql_real_escape_string () CILS MySQL's library function mysql_escape_string, which prepends backslashes to the following characters: NULL, x00, n, r, ', "and x1a

Mysql_escape_string () does not escape % and _.

This function is identical to mysql_real_escape_string () handle T that mysql_real_escape_string () takes a connection handler and escapes the string according to the current character set. mysql_escape_string () does not take a connection argument and does not respect the current charset setting.

 

The http://www.bkjia.com/PHPjc/752406.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752406.htmlTechArticlemysql_real_escape_string () function escapes special characters in strings used in SQL statements. The following characters are affected: if x00 n r'x1a is successful, the function returns the escape character...

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.