将数据库连接存储在会话变量中

发布于 2024-11-08 19:06:09 字数 618 浏览 3 评论 0原文

可能的重复:
无法在 php 中的会话中传递 mysqli 连接

许多我们编写了需要数据库的 PHP 应用程序;主要是 MySQL,但我经常为技术能力较差的人使用非常小的 MS Access 数据库,这样他们就可以下载调整/保存备份/等等。他们自己(无论这是否正确,我不知道)。

我注意到,连接和运行一些相同的查询花费了大量时间。因此,我有一个有趣的想法:将连接和可能的结果集(大部分是静态的)存储在 $_SESSION 变量中,以减轻用户浏览网站时的负担。

显然这样做需要很多考虑。会话被销毁时关闭连接之类的事情只是一个开始。

我的问题归结为:这真的可能吗?如果是这样,我应该注意哪些事情(除了 会话固定,因为它是自己的问题适用于所有会话)?

Possible Duplicate:
Can't pass mysqli connection in session in php

Many of us have written PHP applications that require databases; mostly MySQL, but I have often used very small MS Access databases for people less technically capable so they can download an tweak them/save backups/etc. on their own (whether this is correct or not, I have no idea).

What I notice, is a lot of time is spent connecting and running some of the same queries. Because of this I had an interesting thought: Storing the connection and possible result sets that are mostly static in a $_SESSION variable to reduce the burden as the user navigates the site.

Obviously doing so requires a lot of consideration. Things like closing the connection when the session is destroyed is just the start.

My question boils down to: Is this really something possible? And if so, what things should I be aware of (besides session fixation, as it is its own problem that applies to all sessions)?

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

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

发布评论

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

评论(4

鹿港巷口少年归 2024-11-15 19:06:09

您无法在会话中存储数据库连接或结果集,因为它们是资源,并且:

某些类型的数据无法序列化,因此无法存储在会话中。它包括资源变量或具有循环引用的对象(即将对自身的引用传递给另一个对象的对象)。

http://php.net/manual/en/intro.session.php< /a>


您可以将结果集提取到普通数组中,并将其像任何其他变量一样存储在会话中。无论如何,这将是一个相当典型的会话用例。请注意不要在会话中存储太多数据,因为这可能比从数据库中获取数据更费力。

You can't store database connections or result sets in the session, since those are resources, and:

Some types of data can not be serialized thus stored in sessions. It includes resource variables or objects with circular references (i.e. objects which passes a reference to itself to another object).

http://php.net/manual/en/intro.session.php

You can extract a result set into a normal array and store that in the session like any other variable. That would be a fairly typical use case for sessions anyway. Just be careful not to store too much data in the session, as that can become more taxing than fetching it from the database.

彩扇题诗 2024-11-15 19:06:09

即使您可以做到这一点(资源与数据),这也是一个坏主意。您最终会遇到大量并发打开连接,这将很快耗尽您的最大连接...特别是如果其生命周期超出 100 毫秒(取决于您的查询)到 20 分钟或更长。在开放连接的情况下,像 MySQL 这样的东西也将无法正确重置其内存分配,整个系统就会陷入困境。简而言之,这不是数据库的用途,除非代码的唯一使用者是单个用户。

作为替代方案,我强烈推荐专门为减少数据库负载并消除连接时间而设计的缓存技术。使用最简单的 memcached 之类的东西将显着提高性能,并且您将能够准确指定有多少系统资源进入缓存,同时让数据库在需要时完成获取数据的工作到。

Even if you could do this (resource vs. data), this is a bad idea. You'll wind up with lots of concurrent open connections, which will blow your max connections very quickly... especially if its lifecycle is expanded beyond sub 100ms (depending on your queries) to 20 minutes or more. With open connections, something like MySQL also won't be able to reset its memory allocations properly, and the whole system sort of goes to hell. In short, this is not what DBs are for unless the only consumer of your code will be a single user.

As an alternative, I'd highly recommend caching technologies which are designed specifically to reduce database load and obviate connection times. Using something like, at its simplest, memcached will dramatically improve performance all the way around, and you'll be able to specify exactly how many system resources go into the cache -- while letting the database do its job of getting data when it needs to.

春夜浅 2024-11-15 19:06:09

您可以检查连接部分的永久连接。
http://php.net/manual/en/function.mysql-pconnect.php

You can check permanent connections for the connection part.
http://php.net/manual/en/function.mysql-pconnect.php

雨轻弹 2024-11-15 19:06:09

您应该将它们放在某些配置文件中以便更好地使用。会话是针对特定会话的,而不是针对全局的。

You should those in some config file for better use. Sessions are for specific sessions and not for global.

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