php, mysql server has gone away -
What is, when you connect to DB on line before line, and you still have "MySQL server gone" Away "?
Check this example code:
mysql_connect (" localhost "," xxx "," xxx ") or die (mysql_error ()); mysql_select_db ("Xxx") or die (mysql_error ()); $ Sql = mysql_query ("db from select id"); while ($ data = mysql_fetch_assoc ($ sql)) {$ id [] = $ data [id] ;} Foreach ($ id $ as ID) {$ content = file_get_contents ("http://www.id.com/?id=$id"); if (stristr ($ content, "id is ok man" )) {Mysql_connect ("localhost", "xxx", "xxx") or die (mysql_er Mysql_select_db ("xxx") or die (mysql_error ()); Mysql_query ("UPDATE db set status = 'OK' where id = '$ id'");}} MySQL server has gone away, I think BTW I need to connect in foreachloop in foreach loop because it finds some time ago (like 1-2 minutes) to update, and After that I can be sure that I've got mysql server.
I assume your problem that the script may be executed for a long time before sending the first UPDATE query .
You should check the wait_timeout value in my.cnf. You can try to piece the code to connect again:
if (! Mysql_ping ($$) Conn)) // // Here's the main move, you close the connection (Even if it is not currently working) properly reproduce for it. Mysql_close ($ Conn); $ Conn = mysql_connect ('localhost', 'user', 'pass'); Mysql_select_db ('Database', $ Conn); } This will be done with your code:
mysql_connect ("localhost", "xxx", "xxx") or die (mysql_error () ); Mysql_select_db ("xxx") or die (mysql_error ()); $ Sql = mysql_query ("Select DB to ID"); While ($ data = mysql_fetch_assoc ($ sql)) {if (! Mysql_ping ()) {// Here's a big move, you have to close the connection (even if it is not currently working) mysql_close (); Mysql_connect ("localhost", "xxx", "xxx") or die (mysql_error ()); Mysql_select_db ("xxx") or die (mysql_error ()); } $ Id [] = $ data ['id']; $ Content = file_get_contents ("http://www.id.com/?id=$id"); If (stristr ($ content, "id is ok man")) {mysql_query ("UPDATE db SET status = 'OK' where id = '$ id'"); }}
Comments
Post a Comment