Search code examples
phpwmic

php error handling within a while loop


im having following code to get the battary status using WMIC commands.im getting our network all IP addreses from mysql database.when code runs..if there is remote computer which RPC server not available it throw a fatal error.what i want is if there is any error with a IP,dispaly that IP and custom error msg and continue to next IP without stopping from there.

$sel="select * from nmap_hosts_xml";
    $sel_r=mysql_query($sel);
    while($row1=mysql_fetch_array($sel_r))
    {
        $ip=$row1['address_addr'];
        $os=$row1['os_osmatch_name'];


        $obj = new COM ( "WbemScripting.SWbemLocator" ); 
$obj=$obj->ConnectServer($ip, "root/CIMV2","domain/Username","Password");
$battery=   $obj->ExecQuery("Select * from Win32_Battery");

foreach ( $battery as $wmi_bat )
{
    $bat = $wmi_bat->BatteryStatus;
}

echo $ip." ".$bat."<br>";

    }

Solution

  • Add a try/catch block inside the loop.