php mysql escape method

藏色散人
Release: 2023-03-08 06:16:01
Original
4509 people have browsed it

php mysql escape method: 1. Use the mysql library function "mysql_escape_string" or "mysqli_real_escape_string" to escape, the syntax is "mysql_escape_string($str)" or "mysqli_real_escape_string($str)"; 2. Use escape The defined function "addslashes()" is escaped, and the syntax is "addslashes($str)".

php mysql escape method

#The operating environment of this article: Windows 7 system, PHP8, Dell G3 computer.

php accesses the mysql database to escape special characters in the string

Escape special characters: single quotes ('), double quotes ("), backslashes (\) to facilitate database query

Method 1: Use the mysql library function

PHP version is before 7.0:

mysql_escape_string ( string $unescaped_string ) : string
Copy after login

PHP version is before After 7.0:

mysqli_real_escape_string ( mysqli $link , string $escapestr ) : string
Copy after login

Method 2: Use the escape function addslashes()

Suitable versions PHP4, PHP5, PHP7

addslashes ( string $str ) : string
Copy after login

PHP before 5.4 The directive magic_quotes_gpc is on by default. In fact, all GET, POST and COOKIE data are addedlashes(). Do not use addslashes() on strings that have been escaped by magic_quotes_gpc, because this will cause double-level escaping. When this happens, you can use the function get_magic_quotes_gpc() to detect it. That is, when get_magic_quotes_gpc() returns false, use addslashes() to escape special characters. The example is as follows:

function myaddslashes($data)
{
    if(false == get_magic_quotes_gpc())
    {
        return addslashes($data);//未启用魔术引用时,转义特殊字符
    }
    return $data;
}
Copy after login

Recommended: "PHP Video tutorial

The above is the detailed content of php mysql escape method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template