Quotes in MySQL: Single, Double, and Backticks – A Complete Breakdown

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Do Quotes in MySQL Drive You Crazy?

\n\n

If you've ever written SQL queries in PHP, you've probably faced confusion: when to use single quotes (' '), when to use double quotes (" "), and when to use those mysterious backticks (` `)? One wrong character — and instead of data, you get a MySQL syntax error or, even worse, a security hole.

\n\n

This topic is one of the most popular questions on Stack Overflow (over 2 million views!). And for good reason. Even experienced developers sometimes get confused. Today, we will settle this once and for all. You will learn:

\n\n
    \n
  • What each type of quote in MySQL is responsible for
  • \n
  • How to properly escape data in PHP
  • \n
  • Why double quotes are evil (almost always)
  • \n
  • How to protect against SQL injections
  • \n
\n\n

Let's go! 🚀

\n\n

Anatomy of a MySQL Query: Three Types of Quotes

\n\n

In MySQL, there are three different characters that look like quotes but perform completely different tasks:

\n\n

1. Single Quotes (' ') — For String Values

\n\n

When to use: always, when you are passing a string (text, date, email, password).

\n\n
SELECT * FROM users WHERE name = 'Ivan';\nINSERT INTO articles (title) VALUES ('MySQL for Dummies');
\n\n

MySQL treats everything inside single quotes as a string literal. This means you are telling the database: "Here is a value, do not try to interpret it as a column name or command."

\n\n

2. Double Quotes (" ") — Almost the Same, But with a Nuance

\n\n

In standard SQL, double quotes are used for identifiers (table and column names). However, MySQL by default operates in a mode where double quotes are equivalent to single quotes. This often causes confusion.

\n\n
-- This will work, but it's better not to do it\nSELECT * FROM users WHERE name = "Maria";
\n\n

The Problem: if you enable the strict ANSI_QUOTES mode, double quotes will start working like backticks. Your code will break. Therefore:

\n\n

Rule #1: Use single quotes for strings. Always. Double quotes — only if you know exactly what you are doing, or for compatibility with other DBMS.

\n\n

3. Backticks (` `) — For Table and Column Names

\n\n

Backticks are identifiers. They are needed to:

\n\n
    \n
  • Use reserved words as names (bad practice, but it happens)
  • \n
  • Work with names containing spaces or special characters
  • \n
  • Avoid conflicts with MySQL keywords
  • \n
\n\n
-- Bad: 'select' is a reserved word\nSELECT * FROM select;\n\n-- Good: escape with backticks\nSELECT * FROM `select`;\n\n-- Mandatory: name with a space\nSELECT `user name` FROM users;\n\n-- Recommended: always escape table and column names\nSELECT `id`, `name` FROM `users` WHERE `status` = 'active';
\n\n

Important: Backticks CANNOT be used for values. This will cause an error.

\n\n
-- ERROR: MySQL will think 'Ivan' is a column name\nSELECT * FROM users WHERE name = `Ivan`;
\n\n

PHP + MySQL: How Not to Get Confused with Quotes

\n\n

Now for the interesting part — how this all works in PHP. PHP itself uses single and double quotes for strings, and it's easy to make a mistake here.

\n\n

Case #1: Direct Variable Insertion (Dangerous!)

\n\n
// BAD: vulnerable to SQL injections\n$name = "Ivan'; DROP TABLE users; --";\n$query = "SELECT * FROM users WHERE name = '$name'";\n// Result: SELECT * FROM users WHERE name = 'Ivan'; DROP TABLE users; --'
\n\n

This is a classic SQL injection. Never do this!

\n\n

Case #2: Escaping via mysqli_real_escape_string (Better, but Not Ideal)

\n\n

Blogs

Book Recommendations