有没有一种方法可以使用PHP PDO :: quote方法而不创建连接?

编程入门 行业动态 更新时间:2024-10-26 12:21:28
本文介绍了有没有一种方法可以使用PHP PDO :: quote方法而不创建连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在使用 PDO :: quote 来生成SQL语句.我使用生成的SQL创建memcached密钥.我希望避免仅出于转义SQL值的目的而创建连接.

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.

使用PDO :: quote方法需要建立连接的目的是什么?

What is the purpose of needing to establish a connection to use PDO::quote method and can it be avoided?

推荐答案

如果您知道将使用该SQL的驱动程序,则只需编写自己的函数即可,例如

If you know the driver that the SQL is going to be used with, just write a function of your own, e.g.

/** * @param {string|int|null} $data * @return string */ function quote ($data) { if (is_float($data) || is_int($data)) { return $data; } else if (is_string($data)) { return '\'' . addslashes($data) . '\''; } else if ($data) { throw new Exception('Invalid data type.'); } else { return 'NULL'; } }

一个示例用例:您有一个CLI程序,该程序用于生成SQL代码,稍后将由另一个程序执行.在这种情况下,建立MySQL连接是多余的,并且可能是不必要的.

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.

更多推荐

有没有一种方法可以使用PHP PDO :: quote方法而不创建连接?

本文发布于:2023-11-23 18:22:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622436.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   而不   可以使用   PHP   PDO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!