如何跨不同页面使用mysqli持久连接 [英] How to use mysqli persistent connection across different pages
问题描述
我有一个Web应用程序,该应用程序使用不同的PHP
页面来处理代码.
I have a web app that uses different PHP
pages to process code.
在每个页面的顶部,我打开一个mysqli
到同一数据库的连接(使用相同的用户/密码):
At the top of each page, I open a mysqli
connection to the same database (with the same user/password):
$link = mysqli_connect("$mysql_server", "$mysql_user", "$mysql_pw", "$mysql_db");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
我知道最好使用单个持久连接.如何在所有页面上启用此功能?
I understand it's better to use a single persistent connection. How can I enable this across all pages?
像第一次连接使用$link = mysqli_connect("p:" . $mysql_server, "$mysql_user", "$mysql_pw", "$mysql_db");
一样容易吗?
Is it has easy as just using $link = mysqli_connect("p:" . $mysql_server, "$mysql_user", "$mysql_pw", "$mysql_db");
for the first connection?
在什么情况下该连接会关闭?
Under what circumstances would this connection close?
推荐答案
$db = mysqli_connect("p:notlocalhost","notroot","","catalog") or die("Error " . mysqli_error($link));
它是"p:"-在主机名之前添加时,将创建持久连接.以后可以在不同的页面上重复使用.您仍然在每个页面上调用相同的构造函数.
It is "p:" - when prepended before hostname, creates persistent connection. That is later reused on different pages. You still call same constructor on every page.
这篇关于如何跨不同页面使用mysqli持久连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!