简体   繁体   中英

Type of apostrophe in mysql/php

I came across this example of a query in mysql:

SELECT `id`, `food`, `calories`, `healthy_unhealthy` FROM `food` WHERE 1  //works

Typing it into Codelobster, it comes out like this:

$query = "SELECT 'id', 'food', 'calories', 'healthy_unhealthy' FROM 'food'"; //fails

The apostrophes are different. And it fails with:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''food'' at line

If I take the one that works and place it into the $query, it works.

$query = "SELECT `id`, `food`, `calories`, `healthy_unhealthy` FROM `food`"; //works

If one does this on php editors, the apostrophe does not work, because it is different. Just that change alone makes it not work. Is there a key I can use for that style or a setting in a php editor?

the backtick is mysql syntax to wrap table names and tables to explicitly identify them as such. The regular single quote is for normal string delimiters. The difference is for example

`id`

references a column while

'id'

is just a string. Alternatively you can use NO backtick for column or table names, as long as they aren't mysql reserved words (which you shouldn't be doing anyways)

For example if you have a column named foo in a table named bar you can do

$sql = "select `foo` from `bar`"; 

or

$sql = "select foo from bar";

But if you have for example a column named count , then you MUST wrap it in backticks because count is a reserved word in mysql:

$sql = "select `count` from bar"; 

..but as mentioned, you shouldn't name your columns or tables a reserved word anyways. It's bad practice. While mysql allows for this if you wrap in backticks, not all databases support this, so it would make your query less portable.

` Are called backquotes and they make whatever is interpreted between them interpretted 100% literally. This could actually allow you to have characters with syntactic meaning actually used as a table name or db name (among other things). Although it is bad practice to use characters with syntactic meaning in table names and such, automated programs have to take that into account and therefore enclose all table names and column names and ect in backquotes.

Backticks are used to escape reserved words in MySQL, eg if you had a field named select in your table, then

SELECT select
FROM ...

would be a syntax error, because of MySQL not being able to tell what's a field and what's part of the SQL "scaffolding". But if you use backticks:

SELECT `select`
FROM ...

then it tells MySQl that select is a field (or table name) and is NOT to be treated as an sql keyword.

Using single ' quotes, you make things into a string:

SELECT 'select'
FROM ...

Now that 'select' is the way it is, you're selecting a literal string containing the letters s , e , l , etc... It is treated as a string, and not as an sql keyword, or a field/table name.

That means

SELECT ...
FROM 'food'

fails, because you're trying to select from a literal string. It's not a table name, and not selectable from.

The key depends on your keyboard layout, but in general it is an accent grave (`).

More information here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM