in reply to Lost connection to MySQL server

You can't execute a second query while still fetching the results of an earlier query. (At least, this was the case a year ago when I last tried that). You might want to use $dbh->selectall_arrayref or $dbh->selectall_hashref to fetch the data of your first query.
Also, You are passing a parameter ($region_id) to the second query when executing it, but no placeholders are defined in the second query ($sql_statement).

Replies are listed 'Best First'.
Re^2: Lost connection to MySQL server
by spstansbury (Monk) on Aug 30, 2011 at 16:23 UTC

    Thank you - that's an excellent lead. I'll give the $dbh->selectall_arrayref a shot.

    Re: the placeholder - thanks for the catch, but I had been too sloppy in editing the original code. There is indeed a placeholder:

    my $sql_region_id = "select distinct k.id from config.object o join config.location l on l.id=o.location join config.location k on k.id=l.region order by k.id desc"; my $sql_statement = "select x.ip, m.modemmacaddress, m.date_time, m.cmtsrxsnr, m.cmtsrxpower, m.modemrxsnr, m.modemrxpower, m.modemtxpower, m.microreflections, m.corrected, m.uncorrectable, a.node, a.addr, a.street, a.city, a.state, a.zip5, k.name, i.ifDescr from config.object o join config.location l on l.id = o.location join config.location k on k.id = l.region and k.id = ? join nemos.arp x on o.id = x.object join modem_hist.mh_day_0 m on m.cmtsid = x.object and m.modemmacaddre +ss = x.mac join cust.equipment e on e.mac = x.mac join cust.addr a on a.acct = e.acct join config.ifdrum i on i.object=o.id and i.ifdrum = x.ifdrum where o.community is not null and i.ifindex > 0 and (m.modemtxpower < -15 or m.modemtxpower > 55) or (m.modemrxsnr < 28) or (m.modemrxpower < -12 or m.modemrxpower > 12) or (m.cmtsrxsnr < 25) or (m.microreflections > 40) "; unless ( $region_cursor = $dbh->prepare( "$sql_region_id" )){ exit 1; } unless ( $region_cursor->execute ) { exit 2; } $region_cursor->bind_columns(\($region_id)); while( $region_cursor->fetch() ){ unless ( $cursor = $dbh->prepare( "$sql_statement" )) { exit 1; } unless ( $cursor->execute($region_id) ) { exit 2; } while( $cursor->fetch() ) { do stuff... } }
Re^2: Lost connection to MySQL server
by locked_user sundialsvc4 (Abbot) on Aug 31, 2011 at 01:08 UTC

    I do not find it to be the case, i.e. categorically, that “you can’t execute another query while you are fetching the results of an earlier one.”   (In fact, I just did it.)   As long as you are careful to use two separate statement-handles, it is perfectly okay to, for example, loop through one query and select each of the records in turn that are mentioned in that query.

    I am therefore quite sure that I misunderstood the subtle point you were trying to make – which, I am entirely certain, must indeed have been correct or of course you wouldn’t have said it.   Therefore, could you please clarify your point?   An example of the specific “gotcha” that you are elucidating?   Thanks.

      Perhaps Neighbour mixed up MySQL and MS-SQL? MS SQL Server's protocol is limited to one active statement handle per connection. (Recent versions add some dirty tricks to overcome this limit, but they require non-portable, specially crafted code.) DBI has no such limits, neither has DBD::ODBC; and I can't find a hint in the documentation of DBD::mysql that it is limited to a single active statement.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        You're right..my bad. That's only an issue with MS-SQL which is not what spstansbury is using.