php 会自动重新连接到 MySQL 吗?
$con = mysql_connect("localhost:".$LOCAL_DB_PORT, $LOCAL_DB_USER, $LOCAL_DB_PASS);
mysql_select_db("hr", $con);
mysql_query("set names utf8", $con);
while(true)
{
do_stuff($con);
sleep(50);
}
如果连接在 50 秒内超时,$con 仍然有效吗?
$con = mysql_connect("localhost:".$LOCAL_DB_PORT, $LOCAL_DB_USER, $LOCAL_DB_PASS);
mysql_select_db("hr", $con);
mysql_query("set names utf8", $con);
while(true)
{
do_stuff($con);
sleep(50);
}
If connection timesout in 50 seconds,will $con still work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果连接超时,则无法工作。
要回答评论中的问题,解决问题,请参阅 php.net 手册页 mysql_connect()< /a> 上面写着:
因此,如果您想确保始终有一个打开的连接,只需在用 sleep() 替换的代码执行完毕后尝试打开一个具有相同参数的新连接。
If the connection times out, it won't work.
To answer the question from the comment, to cope with the problem, refer to php.net manual page for mysql_connect() which says:
so if you want to make sure you always have an open connection, just try to open a new one with the same arguments after the code you substituted with sleep() is done executing.
简单的答案:你为什么不尝试一下呢?
我相信codeburger是正确的:如果MySQL连接超时,那么它就消失了。您可以使用 mysql_pconnect 建立持久连接。您需要在每次睡眠后调用该函数,但它将使用现有连接,从而节省开销。
Simple answer: why don't you try it and see?
I believe codeburger is correct: if the MySQL connection times out, then it's gone. You could use a persistent connection with mysql_pconnect. You will need to call that function after sleep each time but it will use an existing connection, saving overhead.