Home > Database > Mysql Tutorial > mysql escape character

mysql escape character

WBOY
Release: 2023-05-13 20:43:06
Original
1751 people have browsed it

MySQL escape characters are symbols or combinations used to represent special characters in MySQL. These symbols can be used to protect data from being misinterpreted as part of an SQL statement. The use of MySQL escape characters is very simple, but they should be used with care when writing SQL statements. This article will introduce some common MySQL escape characters.

  1. : Backslash
    Backslash is one of the most commonly used escape characters in MySQL. When we want to use single or double quotes in a query, we need to escape these symbols using backslashes. For example:
SELECT * FROM users WHERE name = 'Tom';
Copy after login

If there is a single quote in the name, such as "Tom's Restaurant", it needs to be escaped with a backslash:

SELECT * FROM users WHERE name = 'Tom's Restaurant';
Copy after login

Similarly, if we need to If you use double quotes in a string, you need to escape it with a backslash:

SELECT * FROM users WHERE description = "This is a "very nice" restaurant";
Copy after login
  1. %: The percent sign
    The percent sign is used to represent wildcard characters in MySQL. For example, if you want to find all names starting with the letter "T", you can use the following query:
SELECT * FROM users WHERE name LIKE 'T%';
Copy after login

This query will return all names starting with "T". In this case, the percent sign is used as a wildcard to match a string of any length. If we want to find all names starting with "TOM", we can use the following query:

SELECT * FROM users WHERE name LIKE 'TOM%';
Copy after login
  1. _: Underscore
    Underscore can also be used as a wildcard character. However, underscore only matches a single character. For example, if you want to find all people with two letters in their name, you can use the following query:
SELECT * FROM users WHERE name LIKE '__';
Copy after login

Note that in this case, the double underscore is used as a wildcard to match any two letters. character string.

  1. : Null character
    The null character is a special character that represents a null value or empty string. In MySQL, when we need to insert an empty string or find a null value, we can use

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

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