PHPmysql_real_escape_string () function _ PHP Tutorial

Source: Internet
Author: User
PHPmysql_real_escape_string () function. 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 x00nrx1a is successful, the function is defined and used.
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)
Parameter description
String is required. Specifies the string to be escaped.
Connection is 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
$ Con= mysql_connect ("localhost", "hello", "321 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}

// Code for obtaining the user name and password

// Escape the username and password for use in SQL
$ User = mysql_real_escape_string ($ user );
$ Pwd = mysql_real_escape_string ($ pwd );

$ SQL = "SELECT * FROM users WHERE
User = '". $ user."' AND password = '". $ pwd ."'"

// More code

Mysql_close ($ con );
?>
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:
$ Con= mysql_connect ("localhost", "hello", "321 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}

$ SQL = "SELECT * FROM users
WHERE user = '{$ _ POST ['user']}'
AND password = '{$ _ POST ['pwd']}' ";
Mysql_query ($ SQL );

// Do not check the user name and password. // it can be any content entered by the user, for example:
$ _ POST ['user'] = 'John ';
$ _ POST ['pwd'] = "'OR'' = '";

// Some code...

Mysql_close ($ con );
?>
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:
Function check_input ($ value)
{
// Remove the slash
If (get_magic_quotes_gpc ())
{
$ Value = stripslashes ($ value );
}
// If it is not a number, enclose it with quotation marks.
If (! Is_numeric ($ value ))
{
$ Value = "'". mysql_real_escape_string ($ value )."'";
}
Return $ value;
}

$ Con= mysql_connect ("localhost", "hello", "321 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}

// Perform a safe SQL statement
$ User = check_input ($ _ POST ['user']);
$ Pwd = check_input ($ _ POST ['pwd']);
$ SQL = "SELECT * FROM users WHERE
User = $ user AND password = $ pwd ";

Mysql_query ($ SQL );

Mysql_close ($ con );
?>

Author "Shore"

Use the mysql_real_escape_string () function to escape special characters in strings used in SQL statements. The following characters are affected: \ x00 \ n \ r \ x1a. if the operation is successful, this function returns...

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.