thatguy
thatguy

Reputation: 837

Mysql Server has gone away error on PHP script

I've wrote a script to batch process domains and retrieve data on each one. For each domain retrieved, it connects to a remote page via curl and retrieves the data required for 30 domains at a time.

This page typical takes between 2 - 3 mins to load and return the curl result, at this point, the details are parsed and placed into an array (page rank tools function).

Upon running this script via CRON, I keep getting the error 'MySQL server has gone away'.

Can anyone tell me if I'm missing something obvious that could be causing this?

// script dies after 4 mins in time for next cron to start
set_time_limit(240);

include('../include_prehead.php');

$sql = "SELECT id, url FROM domains WHERE (provider_id = 9 OR provider_id = 10) AND google_page_rank IS NULL LIMIT 30";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

do {
$url_list[$row['id']] = $row['url'];
} while ($row = mysql_fetch_assoc($result));

// curl domain information page  - typically takes about 3 minutes
$pr = page_rank_tools($url_list);



foreach ($pr AS $p) {
// each domain
if (isset($p['google_page_rank']) && isset($p['alexa_rank']) && isset($p['links_in_yahoo']) && isset($p['links_in_google'])) {

$sql = "UPDATE domains SET google_page_rank = '".$p['google_page_rank']."' , alexa_rank = '".$p['alexa_rank']."' , links_in_yahoo = '".$p['links_in_yahoo']."' , links_in_google = '".$p['links_in_google']."' WHERE id = '".$p['id']."'";


mysql_query($sql) or die(mysql_error());

}
}

Thanks

CJ

Upvotes: 1

Views: 12364

Answers (3)

Deniss Kozlovs
Deniss Kozlovs

Reputation: 4841

This happens because MySQL connection has its own timeout and while you are parsing your pages, well, it ends. You can try to increase this timeout with

ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);

(as mentioned in MySQL server has gone away - in exactly 60 seconds)

Or just call mysql_connect() again.

Upvotes: 5

Dan Hanly
Dan Hanly

Reputation: 7839

There are many reasons why this error occurs. See a list here, it may be something you can fix quite easily MySQL Server has gone away

Upvotes: 0

ajreal
ajreal

Reputation: 47321

Because the curl take too long time, you can consider to connect again your database before entering the LOOP for update

Upvotes: 2

Related Questions