Question
I am learning how to write MySQL queries more clearly and consistently. So far, I have been using single quotes, double quotes, and backticks interchangeably without a clear rule.
For example:
$query = 'INSERT INTO table (id, col1, col2) VALUES (NULL, val1, val2)';
What is the standard way to use single quotes, double quotes, and backticks in MySQL?
Also, in this example, assume that table, col1, val1, and similar values might come from variables. What is the correct and consistent approach in that case?
Short Answer
By the end of this page, you will understand the different roles of single quotes, double quotes, and backticks in MySQL, and how they interact with PHP strings. You will also learn the safest way to include values and identifiers in queries, why prepared statements matter, and how to avoid common quoting mistakes.
Concept
In MySQL, different quote characters mean different things.
- Single quotes
'...'are primarily used for string values in SQL. - Double quotes
"..."may behave like string quotes in MySQL by default, but their behavior can change depending on SQL modes. Because of that, they are usually not the preferred choice for portable SQL string literals. - Backticks
`...`are used for identifiers, such as table names and column names.
This matters because SQL needs to distinguish between:
- data values: things like
'Alice','hello','2025-01-01' - identifiers: names like
users,email,order - SQL keywords: things like
SELECT,INSERT,FROM
For example:
INSERT INTO `users` (`name`, `email`) VALUES ('Alice', );
Mental Model
Think of SQL like filling out a form with three different kinds of labels:
- Backticks are for the names of boxes on the form: table names and column names.
- Single quotes are for the actual text you put inside the boxes.
- Double quotes are confusing in MySQL because they may sometimes act like single quotes, but not always. That is why many developers avoid them in SQL values.
A simple way to remember it:
- Backticks name things
- Single quotes store text
- Prepared statements insert values safely
In PHP, imagine you are putting one note inside another note:
- the outside note is the PHP string
- the inside note is the SQL query
So you must choose PHP quotes that make the SQL easy to write and read.
Syntax and Examples
Core rules
1. Use single quotes for SQL string values
SELECT * FROM `users` WHERE `name` = 'Alice';
2. Use backticks for table and column names when needed
SELECT `id`, `name` FROM `users`;
Backticks are especially useful when:
- an identifier matches a reserved word
- an identifier contains spaces or special characters
- you want explicit clarity
Example:
SELECT `order`, `group` FROM `sales`;
3. Avoid relying on double quotes for SQL string literals
SELECT * FROM `users` WHERE `name` = "Alice";
This may work in some MySQL setups, but it is better to prefer:
SELECT * `users` `name` ;
Step by Step Execution
Consider this PHP example:
$table = 'users';
$name = 'Alice';
$email = 'alice@example.com';
$sql = "INSERT INTO `users` (`name`, `email`) VALUES (?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$name, $email]);
What happens step by step
1. PHP stores the values in variables
$tablecontains'users'$namecontains'Alice'$emailcontains'alice@example.com'
2. PHP creates the SQL string
$sql = "INSERT INTO `users` (`name`, `email`) VALUES (?, ?)";
The SQL text now contains:
Real World Use Cases
1. Inserting form data
When a user submits a registration form, values like name and email should be passed with prepared statements.
$sql = "INSERT INTO `users` (`name`, `email`) VALUES (?, ?)";
2. Filtering rows by text
Searching for a product name or category uses string values.
$sql = "SELECT * FROM `products` WHERE `category` = ?";
3. Working with reserved words
Some legacy databases have column names like order or group. Backticks let MySQL interpret them as identifiers.
SELECT `order`, `group` FROM `reports`;
4. Building admin tools with selectable columns
If a report lets users choose from known columns, developers often validate the chosen column against an allowed list, then place it in the SQL as an identifier.
$allowedColumns = ['name', 'email', 'created_at'];
Only validated identifiers should ever be inserted into SQL.
Real Codebase Usage
In real projects, developers usually follow a few practical patterns.
Prepared statements for values
This is the default pattern for inserts, updates, deletes, and filtered selects.
$sql = "SELECT * FROM `users` WHERE `email` = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$email]);
Validation for identifiers
Table names and column names cannot usually be bound with ? placeholders. So if an identifier comes from user input or configuration, it should be validated first.
$allowedSortColumns = ['name', 'created_at', 'email'];
$sort = in_array($sort, $allowedSortColumns, true) ? $sort : 'name';
$sql = "SELECT * FROM `users` ORDER BY `$sort`";
Guard clauses before querying
Applications often validate input before sending SQL.
Common Mistakes
1. Using backticks for string values
Broken:
SELECT * FROM `users` WHERE `name` = `Alice`;
Why it is wrong:
`Alice`is treated like an identifier, not a string value
Correct:
SELECT * FROM `users` WHERE `name` = 'Alice';
2. Using single quotes for column names
Broken:
SELECT 'name' FROM 'users';
Why it is wrong:
'name'and'users'are string literals, not identifiers
Correct:
SELECT `name` FROM `users`;
Or, if quoting is not needed:
Comparisons
| Symbol | Typical MySQL use | Example | Notes |
|---|---|---|---|
'...' | String literal | 'Alice' | Standard choice for SQL text values |
"..." | Sometimes string literal | "Alice" | Avoid relying on this in MySQL because SQL mode can affect behavior |
`...` | Identifier | `users` | Used for table names and column names |
PHP quotes vs SQL quotes
| Context | Meaning |
|---|
Cheat Sheet
Quick rules
- Use single quotes for SQL string values:
'Alice' - Use backticks for identifiers:
`users`,`email` - Avoid depending on double quotes for SQL strings in MySQL
- Use prepared statements for dynamic values
- Do not use placeholders for table or column names
- If identifiers are dynamic, validate them against an allowlist first
Safe pattern in PHP
$sql = "INSERT INTO `users` (`name`, `email`) VALUES (?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$name, $email]);
Unsafe pattern
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
Identifier examples
SELECT `id`, `name` FROM `users`;
Value examples
FAQ
Should I always use backticks around table and column names in MySQL?
Not always, but many teams do it for consistency. They are most important when identifiers are reserved words or contain unusual characters.
Are double quotes valid for strings in MySQL?
They may work in some MySQL configurations, but single quotes are the clearer and safer standard for string literals.
Can I use prepared statements for table names?
No. Prepared statement placeholders are for values, not identifiers like table names or column names.
What should I do if the table name comes from a variable?
Validate it against a fixed allowlist, then place the validated name into the SQL string.
Should I manually put quotes around values in prepared statements?
No. The database driver handles values for you when you execute the prepared statement.
Is this a MySQL rule or a PHP rule?
Both are involved. PHP controls how the query string is written in your code, and MySQL controls how quotes are interpreted inside the SQL.
Why are backticks not used for values?
Because backticks identify database object names, not text or numeric data.
Mini Project
Description
Build a small PHP script that inserts a new user into a MySQL database safely. This project demonstrates the correct use of SQL identifiers, string values, and prepared statements. It also shows how to handle a dynamic table name safely by validating it first.
Goal
Create a PHP script that inserts a user record using a validated table name and prepared statement values.
Requirements
- Connect to MySQL using PDO.
- Allow the table name to come from a variable, but validate it against an allowed list.
- Insert
nameandemailusing a prepared statement. - Use backticks for identifiers in the SQL string.
- Do not concatenate raw user input directly into the values part of the query.
Keep learning
Related questions
Choosing the Right MySQL Collation for PHP and UTF-8
Learn how MySQL character sets and collations work with PHP, and how to choose a practical UTF-8 setup for web applications.
Convert a PHP Object to an Associative Array
Learn how to convert a PHP object to an associative array, including quick methods, recursion, pitfalls, and practical examples.
Convert a Postman Request to cURL and PHP cURL
Learn how to convert a Postman POST request into a cURL command and use the same request in PHP cURL with headers and body.