Introduction to the use of the mysql_real_escape_string () function in PHP

Source: Internet
Author: User
This article describes the use of PHP in the mysql_real_escape_string () function, the need for friends, refer to the next bar.

Define and use the mysql_real_escape_string () function to escape special characters in the string used in the SQL statement. The following characters are affected: \x00\n\r\ ' "\x1a if successful, the function returns the escaped string. If it fails, it returns false.

Grammar mysql_real_escape_string (string,connection)

Parameter description string required. Specifies the string to be escaped. Connection is optional. Specify MySQL connection. If not specified, the previous connection is used.

Demonstrates that this function escapes special characters in a string and takes into account the current character set of the connection, so it can be used safely for mysql_query (). Hints and Notes

Tip: You can use this function to prevent database attacks.

Here are some examples of mysql_real_escape_string () functions for your reference.

Example 1:

 
      

Example 2, a database attack.

 
      

Then the SQL query becomes: SELECT * from Userswhere user= ' John ' and password= ' or ' = ', that is, any user can log in without entering a valid password.

Example 3:

 
      
  • 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.