检查mysql连接是否有效 [英] check if mysql connection is valid

查看:583
本文介绍了检查mysql连接是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长时间运行的php脚本,这基本上是一个无限循环侦听事件(它的一个xmpp bot),我开始的脚本 nohup php bot.php&



脚本的原始结构如

  $ mysqli = mysqli_connect(...); 

while(1){
if(event1){
//做数据库操作
} else if(event2){
// non database action
echosomething;
}
}



当我运行脚本时,当我回来几个小时后,机器人工作正常,如果我发出event2,但失败,发出event1与数据库相关的错误(PHP致命错误:调用成员函数bind_param()在一个非对象在线n) / p>

如何保持mysql连接有效或者有办法检查mysqli连接是否有效,以便我可以重新连接?

解决方案

您可以ping连接 http://php.net/manual/en/function.mysql-ping.php


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?

解决方案

you can ping connection http://php.net/manual/en/function.mysql-ping.php

这篇关于检查mysql连接是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆