Avatar ·

When to use single quotes, double quotes, and backticks in MySQL queries from PHP?

Hello everyone! I've just started learning PHP and MySQL, and I'm completely confused about quotes. I'm writing a simple script for a blog where I need to output records from a table and then add a new one. It seems simple, but I constantly get confused about which quote to use when.

\n\n

Here's an example of my code. It works, but I'm not sure it's correct:

\n\n
// Fetching data\n$query = "SELECT * FROM posts WHERE id = $post_id";\n$result = mysqli_query($link, $query);\n\n// Inserting a new record\n$title = "My new post";\n$query = 'INSERT INTO posts (title, content) VALUES ("' . $title . '", "' . $content . '")';\n$result = mysqli_query($link, $query);\n
\n\n

I see that there's a mix of single quotes in PHP, double quotes in SQL, and I've also seen backticks somewhere for