- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习编写查询的最佳方法。我也了解保持一致的重要性。到现在为止,我在没有任何实际考虑的情况下随机使用单引号,双引号和反引号。
例:
$query = 'INSERT INTO table (id, col1, col2) VALUES (NULL, val1, val2)';
table
,
col1
,
val1
等可能是变量。
最佳答案
反引号用于表和列标识符,但仅在标识符为MySQL reserved keyword或标识符包含空格字符或超出限制集的字符时才需要使用反引号(请参见下文)。通常建议避免使用保留关键字在可能的情况下用作列或表的标识符,避免引用问题。
单引号应用于VALUES()
列表中的字符串值。 MySQL还为字符串值支持双引号,但是单引号已被其他RDBMS广泛接受,因此使用单引号而不是双引号是一个好习惯。
MySQL还希望DATE
和DATETIME
文字值被单引号括起来,如'2001-01-01 00:00:00'
这样的字符串。有关更多详细信息,请查阅the Date and Time Literals文档,尤其是在日期字符串中使用连字符-
作为分段定界符的替代方法。
因此,使用您的示例,我将对PHP字符串加双引号,并对值'val1', 'val2'
使用单引号。 NULL
是MySQL关键字,是一个特殊的(非)值,因此未加引号。
这些表或列标识符都不是保留字,也不使用需要引号的字符,但是无论如何我都用反引号将它们引号(稍后再说……)。
RDBMS的本机函数(例如,MySQL中的NOW()
)不应该被引用,尽管它们的参数要遵循已经提到的相同的字符串或标识符引用规则。
Backtick (`)table & column ───────┬─────┬──┬──┬──┬────┬──┬────┬──┬────┬──┬───────┐ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓$query = "INSERT INTO `table` (`id`, `col1`, `col2`, `date`, `updated`) VALUES (NULL, 'val1', 'val2', '2001-01-01', NOW())"; ↑↑↑↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑↑↑↑↑ Unquoted keyword ─────┴┴┴┘ │ │ │ │ │ │ │││││Single-quoted (') strings ───────────┴────┴──┴────┘ │ │ │││││Single-quoted (') DATE ───────────────────────────┴──────────┘ │││││Unquoted function ─────────────────────────────────────────┴┴┴┴┘
The quoting patterns for variables do not change, although if you intend to interpolate the variables directly in a string, it must be double-quoted in PHP. Just make sure that you have properly escaped the variables for use in SQL. (It is recommended to use an API supporting prepared statements instead, as protection against SQL injection).
// Same thing with some variable replacements// Here, a variable table name $table is backtick-quoted, and variables// in the VALUES list are single-quoted $query = "INSERT INTO `$table` (`id`, `col1`, `col2`, `date`) VALUES (NULL, '$val1', '$val2', '$date')";
When working with prepared statements, consult the documentation to determine whether or not the statement's placeholders must be quoted. The most popular APIs available in PHP, PDO and MySQLi, expect unquoted placeholders, as do most prepared statement APIs in other languages:
// PDO example with named parameters, unquoted
$query = "INSERT INTO `table` (`id`, `col1`, `col2`, `date`) VALUES (:id, :col1, :col2, :date)";
// MySQLi example with ? parameters, unquoted
$query = "INSERT INTO `table` (`id`, `col1`, `col2`, `date`) VALUES (?, ?, ?, ?)";
[0-9,a-z,A-Z$_]
(基本拉丁字母,数字0-9,美元,下划线)
关于mysql - 何时在MySQL中使用单引号,双引号和反引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56517619/
我正在尝试在引号内使用引号。它在文本框内工作,但在我需要字符串的地方不工作。例如,第一行代码有效,但我需要将字符串作为变量。 pictureBox1.Image = MediaLib.Get["che
我需要找到一种方法来检查字符串中的双引号,以便将输出写入 XML 文档,然后在 Word 中打开。我想出了如何查找像 (') 这样的单引号,但是双引号在我的 XML 文档中引发了错误。 pr
我正在尝试在引号内使用引号。它在文本框内工作,但在我需要字符串的地方不工作。例如,第一行代码有效,但我需要将字符串作为变量。 pictureBox1.Image = MediaLib.Get["che
我定义了一个由动态变量组成的变量 var tempVar = ""; varName输出Black/Blue Mirror + some special character with single a
我在 Jenkins 管道中运行了这个 shell 脚本 def planResults = sh(returnStdout: true, script: "cd $it; PLAN=\$(terra
双引号 ("") 怎么可能有一个方法,即使您实际上没有在 String 中声明它。例如: System.out.println("".length()); "" 是否已经是一个对象,但没有在 Stri
我刚刚开始阅读“Head First Java”这本书。由于下面代码中没有空格的双引号 (""),有一个练习让我感到困惑。我想我明白了,但看起来这本书并没有解释它,我想确保我是正确的。 下面代码中双引
我有一个名为 myfield 的字段,其中包含以下字符串: {'Content-Language': 'en', 'X-Frame-Options': 'SAMEORIGIN', 'X-Generat
这是一个 nodejs 正则表达式,它将引号替换为 json 解析器接受的新模式。 var someStr = "this string has one single quote ': this st
当查询列表并将值放入变量并尝试在另一个脚本中使用该变量时,它确实获得了所需的格式。 脚本 1: cilist=$(opr-ci-list.sh -view_name TN_UD_REFRESH_MRE
我正在尝试在 powershell 中调用 curl 命令并传递一些 JSON 信息。 这是我的命令: curl -X POST -u username:password -H "Content-Ty
我使用 terraform 创建了一个 YML 管道。 它使用脚本任务并在输出中返回 Web 应用程序名称 steps: - script: | [......] terraform
我的项目由Docker包装,并在2种环境中运行:代理后面的或没有代理的。 在这种情况下,我使用php:apache镜像并通过pecl安装ext,因此必须手动设置代理 FROM php:apache R
我是 Clang-Format 的新手。 我使用 ./bin/clang-format -style=google -dump-config > .clang-format 作为我的基础 .clang
我试图在从数据库读取数据后生成 CSV 文件。现在单个数据可以包含逗号、单引号和双引号。 请告诉我如何在正则表达式的帮助下处理这种情况。 最佳答案 您可以为 CSV 文件使用不同的分隔符吗?也许是一个
我全新安装了 janus vim 插件和 macvim。 由于某种原因,当我在 macvim 中打开 .json 文件时,.json 文件中的键或值周围没有双引号。当我向下滚动文件的行时,双引号会显示
我的属性结构如下: my { property { item { "1" { value="some value"
这个问题已经有答案了: How to escape single quotes in MySQL (19 个回答) 已关闭 5 年前。 考虑这个表 CREATE TABLE name ( nom VA
我面临 SyntaxError: unterminated string literal* 尝试在文本区域中设置值时出错。 这里,文本值是动态的,从后端接收,可以包含任何字符。 经过我的第一次调查,我
我正在尝试设置包含单引号的属性值: var attr_value = "It's not working"; var html = "Text"; $('body').html(html); 但是,我
我是一名优秀的程序员,十分优秀!