无法在 php 会话中传递 mysqli 连接 [英] Can't pass mysqli connection in session in php
问题描述
我正在尝试通过 mysqli 连接为站点上的多个查询传递会话,但是当我尝试执行查询时,它输出警告无法获取 mysqli"
I'm trying to pass a session with mysqli connection for multiple queries on the site, but when I try to do a query it outputs a warning "Couldn't fetch mysqli"
$_SESSION['db']=new mysqli($host,$username,$password,$db);
是否不可能通过会话传递 mysqli 连接引用?有什么不同的使用方法吗?
Is it impossible to pass a mysqli connection reference thru session? is there a different method to use?
推荐答案
是的,这显然是不可能的.
Yes, it is explicitly impossible.
请参阅 此处的 PHP 文档 在突出显示的警告中提及:某些类型的数据无法序列化,因此存储在会话中.它包括资源变量或具有循环引用的对象(即,将对其自身的引用传递给另一个对象的对象)."
See PHP Documentation here mentioning in a highlighted Warning: "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)."
MySQL 连接就是这样一种资源.
MySQL connections are one such kind of resource.
您必须在每次页面运行时重新连接.
You have to reconnect on each page run.
如果您可以通过 依赖连接池,这并没有听起来那么糟糕mysql_pconnect(),但首先在 这篇文章.
This is not as bad as it sounds if you can rely on connection pooling via mysql_pconnect(), but first see more background info on mysql_pconnect() in this article.
这篇关于无法在 php 会话中传递 mysqli 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!