php - 何时/如何在 mysql_query 中使用反引号和单引号?

标签 php mysql sql

mysql_query("select ip from table1 where ip=$ip",$conn);*NOT work*  
mysql_query("select ip from table1 where ip='$ip'",$conn);*work*

有什么问题吗?

最佳答案

根据 Schema Object Names 记录:

Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers.

[ deletia ]

An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. (Exception: A reserved word that follows a period in a qualified name must be an identifier, so it need not be quoted.) Reserved words are listed at Section 9.3, “Reserved Words”.

[ deletia ]

The identifier quote character is the backtick (“`”):

mysql> <strong>SELECT * FROM `select` WHERE `select`.id > 100;</strong>

If the ANSI_QUOTES SQL mode is enabled, it is also permissible to quote identifiers within double quotation marks:

mysql> <strong>CREATE TABLE "test" (col INT);</strong>
ERROR 1064: You have an error in your SQL syntax...
mysql> <strong>SET sql_mode='ANSI_QUOTES';</strong>
mysql> <strong>CREATE TABLE "test" (col INT);</strong>
Query OK, 0 rows affected (0.00 sec)

根据 String Literals 记录:

A string is a sequence of bytes or characters, enclosed within either single quote (“'”) or double quote (“"”) characters. Examples:

'a string'
"another string"
[ deletia ]

If the ANSI_QUOTES SQL mode is enabled, string literals can be quoted only within single quotation marks because a string quoted within double quotation marks is interpreted as an identifier.

Number Literals不应该被引用(尽管 MySQL 通常会处理它们,因为它会执行合适的 type conversion ); Date and Time Literals可能需要引用,具体取决于所使用的格式。

调用 functions必须永远不要被引用,否则它们将被解析为字符串文字。

关于php - 何时/如何在 mysql_query 中使用反引号和单引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22890485/

相关文章:

php - 使用 php 从 mysql 查询中排序数据有困难

php - ACL ROLE_ADMIN 不起作用

PHP:基于 JWT 的身份验证

php - 使用带绑定(bind)的 PHP 准备语句显示来自 mySQL 的行

java - 在android 4.0中使用mysql调用soap webservice的登录表单

php - 输入mysql php后更新

sql - 是否有可用于 SQL 的 BNF 语法?

php - 在 Laravel 上本地化日期的最佳方法是什么?

java - 如何使用hibernate将文件存储在数据库中

Java与数据库连接