跨多个 PHP 页面传输 MySQL 数据库链接

发布于 2024-07-16 06:36:04 字数 164 浏览 9 评论 0原文

是否有任何技巧可以在多个 PHP 页面之间传输和/或共享资源链接? 我不希望服务器继续连接到数据库只是为了获取同一数据库中同一用户的另一个链接。

请记住,从 mysql_connect() 返回的链接在它所源自的脚本完成执行后会自动关闭。 无论如何,是否可以在每个会话结束时手动关闭它?

Are there any tricks to transferring and/or sharing the Resource Links across multiple PHP pages? I do not want the server to continue connecting to the database just to obtain another link to the same user in the same database.

Remember that the link returned from mysql_connect() is automatically closed after the script it originated on completes executing. Is there anyway to close it manually at the end of each session instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-07-23 06:36:04

PHP 允许持久 mysql 连接,但是有缺点 。 最重要的是,空闲的 apache 子进程最终会闲着,保持空闲的数据库连接打开。 数据库连接占用相当多的内存,因此您实际上只希望它们在实际使用时打开。

如果您的用户每分钟打开一个页面,那么最好在您不使用数据库连接的每一分钟中关闭数据库连接 59 秒,并在需要时重新打开它,而不是持续保持打开状态。

相反,您可能应该研究连接池。

PHP allows persistent mysql connections, but there are drawbacks. Most importantly, idle apache children end up sitting around, holding idle database connections open. Database connections take up a decent amount of memory, so you really only want them open when they're actually being used.

If your user opens one page every minute, it's far better to have the database connection closed for the 59 seconds out of every minute you're not using it, and re-open it when needed, than to hold it open continually.

Instead, you should probably look into connection pooling.

多情癖 2024-07-23 06:36:04

与其他重量级数据库服务器相比,Mysql 的优势之一是连接成本低且设置速度快。

如果您担心需要大量连接来检索保存的信息,您可能需要查看诸如信息缓存之类的内容,或者从磁盘获取信息。 与往常一样,对正在执行的 SQL 调用的数量和类型进行分析,将告诉您比这里的任何人都更多地猜测您下一步应该做什么。

One of the advantages of Mysql over other heavier-weight database servers is that connections are cheap and quick to setup.

If you are concerned about large numbers of connections to retrieve the save information, you may like to look at such things as caching of information instead, or as well as getting the information from disk. As usual, profiling of the number, and type of calls to SQL that are being made, will tell you are great deal more than anyone here guessing at what you should really be doing next.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文