About Mysql queries with single quotes and inserts with single quotes string problems _mysql

Source: Internet
Author: User
Tags mysql query sql injection
MySQL query with quotes and no quotes difference
When the database field ID is an integral type
Select ID from table where id=1
And
Select ID from table where id= ' 1 '
Two SQL is OK, but the first SQL does not have to be implicitly converted, slightly faster than the second SQL

Today, when inserting a single quote string into a MySQL database, nothing is reported as a statement execution failure, and then it is known that single quotes, etc. to escape, can use functions: mysql_real_escape_string and Addslashes functions;
Here's an introduction: Before inserting data into a database, it is first escaped and inserted into the database.

This article is very good to explain the difference between addslashes and mysql_real_escape_string, although many of the domestic PHP coder still rely on addslashes to prevent SQL injection, I suggest that we strengthen the Chinese to prevent SQL injection check. The problem with addslashes is that hackers can use 0xbf27 instead of single quotes, and addslashes only modifies 0xbf27 to 0xbf5c27 as a valid multi-byte character, where the 0xbf5c is still considered single quotes, So addslashes cannot successfully intercept.

Of course, addslashes is also not useless, it is used for single-byte string processing, multibyte characters or use mysql_real_escape_string bar.

Also for examples of GET_MAGIC_QUOTES_GPC in the PHP manual:
Copy Code code as follows:

<?php
if (!GET_MAGIC_QUOTES_GPC ()) {
$lastname = Addslashes ($_post["LastName"]);
} else {
$lastname = $_post[' LastName '];
}
?>

It is better to check the $_post[' LastName ' If the MAGIC_QUOTES_GPC is already open.
Again, the difference between the 2 functions of mysql_real_escape_string and mysql_escape_string:
Mysql_real_escape_string must be available in the case of (PHP 4 >= 4.3.0, PHP 5). Otherwise you can only use mysql_escape_string, the difference is:
Mysql_real_escape_string takes into account the current character set of the connection, and Mysql_escape_string does not consider it.
To sum up
Addslashes () is forcibly added;
Mysql_real_escape_string () will judge the character set, but the PHP version is required;
Mysql_escape_string does not consider the current character set of the connection.
Related Article

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.