You could include in your loop the mysql_ping()
function. This function checks to make sure that the connection is open, and if it is not, it re-connects.
Using your own example, you could do something like:
foreach($array as $value) {
mysql_ping($dbconn);
$sql="insert into collected values('".$value."')";
$res=mysql_query($sql);
//then some extra code.
}
Edit: It should be noted that according to the docs, after MySQL 5.0.14, PHP does not automatically reconnect. If you use a newer version of MySQL you will have to put in your own connection logic, maybe like this (I haven't tested):
function check_dbconn($connection) {
if (!mysql_ping($connection)) {
mysql_close($connection);
$connection = mysql_connect('server', 'username', 'password');
mysql_select_db('db',$connection);
}
return $connection;
}
foreach($array as $value) {
$dbconn = check_dbconn($dbconn);
$sql="insert into collected values('".$value."')";
$res=mysql_query($sql, $dbconn);
//then some extra code.
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…