有没有办法在不创建连接的情况下使用 PHP PDO::quote 方法?
我一直在使用 PDO::quote 生成 SQL 语句。我使用生成的 SQL 创建一个 memcached
键。我想避免仅仅为了转义 SQL 值而创建连接。
使用 PDO::quote 方法需要建立连接的目的是什么?可以避免吗?
I've been using PDO::quote to generate SQL statements. I use the generated SQL to create a memcached
key. I'd like to avoid having to create a connection solely for the purpose of escaping SQL values.
What is the purpose of needing to establish a connection to use PDO::quote method and can it be avoided?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为PDO是一个抽象层,每个数据库的引用方式都不同。 PDO 必须知道您将使用哪个数据库才能使用正确的引用/转义 API 调用。
Because PDO is an abstraction layer, and each database's quoting methods differ. PDO must know WHICH database you're going to be using to be able to use the proper quoting/escaping API calls.
您需要一个连接,因为转义变量的概念仅在要转义的数据库连接上下文中才有意义。不同的字符可能会有不同的解释,例如 MySQL 和 PostgreSQL,或者取决于连接的字符集。因此,在真空中谈论转义变量是没有意义的,您需要一些关于如何解释结果字符串的概念。
也许您可以使用其他东西作为 memcached 的密钥。
You need a connection because the notion of escaping variables only makes sense when in the context of a database connection with respect to which they are to be escaped. Different characters might be interpreted differently, say, by MySQL and PostgreSQL, or depending on the character set of the connection. So it does not make sense to talk about escaping variables in a vacuum, you need some notion of how the resulting string will be interpreted.
Maybe you could use something else as a key to memcached.
如果您知道要使用 SQL 的驱动程序,只需编写自己的函数,例如
示例用例:您有一个 CLI 程序,用于生成 SQL 代码,稍后将由另一个程序执行。在这种情况下,建立 MySQL 连接是多余的,并且可能不需要。
If you know the driver that the SQL is going to be used with, just write a function of your own, e.g.
An example use case: You have a CLI program that is used to generate SQL code that will be executed later by another program. In this scenario, establishing MySQL connection is redundant and might be unwanted.