检查mysql连接是否有效
我有一个长时间运行的 php 脚本,它基本上是一个无限循环监听事件(它是一个 xmpp 机器人),我使用 nohup php bot.php & 启动脚本。
脚本的原始结构就像
$mysqli = mysqli_connect(...);
while(1) {
if(event1) {
// do database action
} else if(event2) {
// non database action
echo "something";
}
}
当我运行脚本时,最初一切正常。当我在几个小时后回来时,如果我发出 event2,但发出 event1 时失败,并出现数据库相关错误(PHP Fatal error: Call to a member function bind_param() on a non-object in on line n),机器人工作
正常我可以保持 mysql 连接有效,还是有办法检查 mysqli 连接是否有效,以便我可以重新连接?
I have a long running php script which is basically an infinite loop listening for events (its an xmpp bot), I start the script with nohup php bot.php &
.
The raw structure of the script is like
$mysqli = mysqli_connect(...);
while(1) {
if(event1) {
// do database action
} else if(event2) {
// non database action
echo "something";
}
}
When I run the script everything works fine initially. When I come back after a few hours the bot works fine if I issue event2 but fails on issuing event1 with a database related error (PHP Fatal error: Call to a member function bind_param() on a non-object in on line n)
How can I keep the mysql connection valid or is there a way to check if the mysqli connection is valid so that i can reconnect otherwise?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以 ping 连接 http://php.net/manual/en/function.mysql -ping.php
you can ping connection http://php.net/manual/en/function.mysql-ping.php