问题描述
我正在尝试通过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连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!