1. <i id='QPcaQ'><tr id='QPcaQ'><dt id='QPcaQ'><q id='QPcaQ'><span id='QPcaQ'><b id='QPcaQ'><form id='QPcaQ'><ins id='QPcaQ'></ins><ul id='QPcaQ'></ul><sub id='QPcaQ'></sub></form><legend id='QPcaQ'></legend><bdo id='QPcaQ'><pre id='QPcaQ'><center id='QPcaQ'></center></pre></bdo></b><th id='QPcaQ'></th></span></q></dt></tr></i><div id='QPcaQ'><tfoot id='QPcaQ'></tfoot><dl id='QPcaQ'><fieldset id='QPcaQ'></fieldset></dl></div>

      <small id='QPcaQ'></small><noframes id='QPcaQ'>

        <bdo id='QPcaQ'></bdo><ul id='QPcaQ'></ul>
    2. <tfoot id='QPcaQ'></tfoot>
    3. <legend id='QPcaQ'><style id='QPcaQ'><dir id='QPcaQ'><q id='QPcaQ'></q></dir></style></legend>

      仅在需要时自动连接到 PDO

      时间:2023-10-04
      • <i id='c6ZAc'><tr id='c6ZAc'><dt id='c6ZAc'><q id='c6ZAc'><span id='c6ZAc'><b id='c6ZAc'><form id='c6ZAc'><ins id='c6ZAc'></ins><ul id='c6ZAc'></ul><sub id='c6ZAc'></sub></form><legend id='c6ZAc'></legend><bdo id='c6ZAc'><pre id='c6ZAc'><center id='c6ZAc'></center></pre></bdo></b><th id='c6ZAc'></th></span></q></dt></tr></i><div id='c6ZAc'><tfoot id='c6ZAc'></tfoot><dl id='c6ZAc'><fieldset id='c6ZAc'></fieldset></dl></div>
            <tbody id='c6ZAc'></tbody>

          <legend id='c6ZAc'><style id='c6ZAc'><dir id='c6ZAc'><q id='c6ZAc'></q></dir></style></legend>

          • <bdo id='c6ZAc'></bdo><ul id='c6ZAc'></ul>

            1. <small id='c6ZAc'></small><noframes id='c6ZAc'>

                <tfoot id='c6ZAc'></tfoot>
                本文介绍了仅在需要时自动连接到 PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一段代码,根据请求的 URL,将包含其他十四个文件之一.这 14 个文件中的一些需要连接到三个不同数据库之一,并且可以随时添加其他文件.

                I have a section of code that depending on the URL requested, will include one of fourteen other files. Some of these fourteen files require a connection to one of three different databases, and additional files can be added at anytime.

                我不想默认打开所有三个数据库的 PDO 连接,因为它浪费资源并且会减慢执行时间.所以我的想法是将所有 SQL 查询包装在一个函数中.第一次在未打开的 PDO 连接上执行查询时,try {} 错误处理程序可以捕获它,找出问题所在(在这种情况下连接不存在),然后打开连接并重新执行询问.这样,数据库只在需要时才连接 - 只要连接字符串(主机、数据库、用户名、密码)都预先定义,我看不出它有任何问题.

                I don't want to open PDO connections by default to all three database as its a waste of resources and will slow the execution time down. So my thought is to wrap all SQL queries within a function. The first time that a query is executed on a non-open PDO connection, the try {} error handler can catch it, find out what the problem was (in this case connection doesnt exist), then open the connection and re-execute the query. That way, the database is only being connected to as and when needed - as long as the connection string (host, database, username, password) are all defined in advance, I can't see any problem in it working.

                但是,我需要继续推进,并且在大约 7 天内无法访问开发箱,所以任何人都可以看出这种情况有什么问题吗?另外,如果没有打开连接,谁能告诉我 handler->errorInfo() 将返回的错误消息?

                However, I need to push on with this, and don't have access to the dev box for about 7 days, so can anyone see any problem with that scenario? Also, can anyone give me the error message that handler->errorInfo() will return if the connection isn't opened?

                推荐答案

                这是正确的想法,但不是它的最佳实现.

                This is the right idea, but not the best implementation of it.

                包装 SQL 操作很好.但是你为什么不这样做呢:

                Wrapping the SQL operations is good. But why don't you do it this way:

                class Wrapper {
                    private static $db;
                
                    public static function someQuery() {
                        $db = self::getDatabase();
                        // now go on to execute the query
                    }
                
                    private static function getDatabase() {
                        if (self::$db === null) {
                            self::$db = // connect here
                        }
                        return self::$db;
                    }
                }
                

                这有很多优点:

                • 允许您在逻辑上将 SQL 操作分组为一个(或多个!)类
                • 如果不需要,不连接到数据库
                • 不依赖(脆弱的)错误检查来正常运行

                在您的特定情况下,您可能应该使用 3 个单独的 Wrapper 类.将所有内容都放在一个类中是可行的(三个不同的 $db 变量),但可能比它的价值更令人困惑.

                In your specific case, you should probably go with 3 separate Wrapper classes. Putting everything into one class is doable (three different $db variables) but probably more confusing than it's worth.

                这篇关于仅在需要时自动连接到 PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:从文本文件中获取数据并将其显示在 html 表中 下一篇:即使我可以从 cli 客户端使用,为什么我不能将

                相关文章

                最新文章

                <small id='6NGVX'></small><noframes id='6NGVX'>

                  <bdo id='6NGVX'></bdo><ul id='6NGVX'></ul>
                <legend id='6NGVX'><style id='6NGVX'><dir id='6NGVX'><q id='6NGVX'></q></dir></style></legend>

                  <tfoot id='6NGVX'></tfoot>
                  <i id='6NGVX'><tr id='6NGVX'><dt id='6NGVX'><q id='6NGVX'><span id='6NGVX'><b id='6NGVX'><form id='6NGVX'><ins id='6NGVX'></ins><ul id='6NGVX'></ul><sub id='6NGVX'></sub></form><legend id='6NGVX'></legend><bdo id='6NGVX'><pre id='6NGVX'><center id='6NGVX'></center></pre></bdo></b><th id='6NGVX'></th></span></q></dt></tr></i><div id='6NGVX'><tfoot id='6NGVX'></tfoot><dl id='6NGVX'><fieldset id='6NGVX'></fieldset></dl></div>