简体   繁体   English

mysql / php中单引号的类型

[英]Type of apostrophe in mysql/php

I came across this example of a query in mysql: 我在mysql中遇到了以下查询示例:

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

Typing it into Codelobster, it comes out like this: 将其输入Codelobster,结果如下:

$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; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''food'' at line 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在行“ food”附近使用

If I take the one that works and place it into the $query, it works. 如果我选择一个可行的并将其放入$ query中,它就会奏效。

$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. 如果在php编辑器上执行此操作,则撇号不起作用,因为它是不同的。 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? 有没有可以用于该样式的键或php编辑器中的设置?

the backtick is mysql syntax to wrap table names and tables to explicitly identify them as such. 反引号是mysql语法,用于包装表名和表以明确地标识它们。 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) 或者,您可以对列或表名使用NO反引号,只要它们不是mysql保留字即可(无论如何您都不应这样做)

For example if you have a column named foo in a table named bar you can do 例如,如果在名为bar的表中有一个名为foo的列,则可以执行

$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: 但是,例如,如果您有一个名为count的列,则必须将其包装在反引号中,因为count是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. 尽管如果使用反引号,mysql允许这样做,但并非所有数据库都支持此功能,因此会使查询的可移植性降低。

` Are called backquotes and they make whatever is interpreted between them interpretted 100% literally. `称为反引号,它们使被解释的内容在字面上100%解释。 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 反引号用于在MySQL中转义保留字,例如,如果表中有一个名为select的字段,则

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". 这将是一个语法错误,因为MySQL无法分辨出什么是字段以及SQL“脚手架”的组成部分。 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. 然后它告诉MySQl select是一个字段(或表名),并且不被视为sql关键字。

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. 现在'select'就是这样,您正在选择一个包含字母sel等的文字字符串...它被视为字符串,而不是sql关键字或字段/表名称。

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 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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