doumiao0498 2012-11-17 23:25
浏览 23
已采纳

MySQL和PHP:何时与MySQL数据库建立连接?

When issuing a MySQL/SQL query from PHP code, a connection with the database must be firstly established.

Would you advise me, please, whether I should establish connection with the database only at the beginning of the user's web session, or upon loading new page, or before every single MySQL/SQL query?

I think that when establishing connection with the database too many times unnecessarily, I will consume unnecessary system resources. I would therefore prefer connecting with the database the minimum necessary times.

  • 写回答

4条回答 默认 最新

  • douba3378 2012-11-17 23:33
    关注

    You need to strike the right balance. Each part has its problems. Let's go over all three possibilities

    Persistent MySQL connection

    Advantages: load-once-and-forget

    Disadvantages: you'll need to make sure to kill any connection that is not active anymore yourself. Also, a dormant connection still uses some resources. Not recommended, as the only good case for it is a lot of requests, at which point you'll most likely want to cluster up your database servers, and persistent connections tend to play havoc with that.

    One-query-one-request model

    Advantages: build-and-forget

    Disadvantages: extreme resource usage.

    The good solution is a right balance. Load up your DBH when you actually need it for the first time, and keep it until your script is over, at which point you should free all resources used by it and terminate it. This guarantees that you'll have done everything using the minimum amount of resources needed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢