gpt4 book ai didi

php - 哪个更好: mysql_connect or mysql_pconnect

转载 作者:IT王子 更新时间:2023-10-28 23:47:16 25 4
gpt4 key购买 nike

我是一个 PHP 新手,正在编写一些脚本来显示数据库中的一些新闻文章,并且想了解一些事情。

  • 要打开与 MySQL 数据库的连接,mysql_connectmysql_pconnect 哪个更好?
  • 使用与数据库的持久连接有哪些优点或缺点?
  • 在什么样的场景下会需要长连接?

最佳答案

如果您要编写网页,则不需要持久连接。它占用了太多资源。使用 mysql_connect。尽量减少数据库连接打开和不使用的时间。打开,获取你想要的,关闭。它不需要在用户阅读时保持打开状态。如果他们响应,最终将使用连接 - 插入/转到另一个页面..

Here关于 NOT USING 在 Web 应用程序中的持久连接

  • When you lock a table, normally it is unlocked when the connection closes, but since persistent connections do not close, any tables you accidentally leave locked will remain locked, and the only way to unlock them is to wait for the connection to timeout or kill the process. The same locking problem occurs with transactions. (See comments below on 23-Apr-2002 & 12-Jul-2003)

  • Normally temporary tables are dropped when the connection closes, but since persistent connections do not close, temporary tables aren't so temporary. If you do not explicitly drop temporary tables when you are done, that table will already exist for a new client reusing the same connection. The same problem occurs with setting session variables. (See comments below on 19-Nov-2004 & 07-Aug-2006)

  • If PHP and MySQL are on the same server or local network, the connection time may be negligible, in which case there is no advantage to persistent connections.

  • Apache does not work well with persistent connections. When it receives a request from a new client, instead of using one of the available children which already has a persistent connection open, it tends to spawn a new child, which must then open a new database connection. This causes excess processes which are just sleeping, wasting resources, and causing errors when you reach your maximum connections, plus it defeats any benefit of persistent connections. (See comments below on 03-Feb-2004, and the footnote at http://devzone.zend.com/node/view/id/686#fn1)

关于php - 哪个更好: mysql_connect or mysql_pconnect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1340859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com