How to use Backslashes () In MySQL
In MySQL, one of the common practices to prevent string literal from special characters, like apostrophes, is to use a backslashes (\) to escape them. If you add a backslash before an apostrophe, MySQL will take it as a mark of escape indicating that the apostrophe shall be dealt with as part of a string rather than as delimiters of strings.
-- Using Backslashes
SELECT * FROM Products WHERE ProductName = 'Men\'s Shirt';
Output:
Explanation: The output retrieves rows from the Products table where the ProductName matches ‘Men’s Shirt‘. The backslash preceding the apostrophe escapes it, ensuring correct interpretation of the string value within the query.
How to Escape Apostrophe in MySQL?
Single quotes are used in MySQL to enclose and identify text data. When dealing with text data that contains an apostrophe, it is important to use correct escaping to avoid SQL injection and other security issues.
In this article, we will discuss several ways to escape an apostrophe in MySQL, which will make your life easier when dealing with text data as well as query execution.