Too many outstanding queries

92 views
Skip to first unread message

Domains

unread,
Sep 8, 2021, 8:52:18 AM9/8/21
to PHRETS
How do I begin to track down this error, I can only make it happen when
I haven't done an update to data in a while and it's doing a large
update. Been using for years and as long as my cron works I don't get
the error but l am in middle of moving servers and was out of date for a
few days.  Any help to clean up my code would be appreciated.

PHP Fatal error:  Uncaught PHRETS\Exceptions\RETSException: Too many
outstanding queries in
/opt/bitnami/wordpress/vendor/troydavisson/phrets/src/Session.php:424

Here is my code for the update. probably a little unorthodox

$updated = 0;
$inserted = 0;

require_once("vendor/autoload.php");

$config = new \PHRETS\Configuration;
$config->setLoginUrl('http://retsiqmatrix.harmls.com/contact/rets/login')
    ->setUsername('')
    ->setPassword('')
    ->setOption('use_post_method',true)
    ->setRetsVersion('1.8');

$rets = new \PHRETS\Session($config);
$connect = $rets->Login();

//get last matrixid and matrixmodified date from last run
$sql = "SELECT lastmatrixid,downloaddate FROM wp99_rets_downloads WHERE
type = 1 ORDER BY downloadid DESC LIMIT 1;";

//neeed to get date and matrix id
if($result = $db->select($sql)) {
    $row = mysqli_fetch_assoc($result);
    $date =  new DateTime($row['downloaddate']);
    $date = date_format($date,'Y-m-d\TH:i:s');
    $starttime = date("Y-m-d H:i:s"); //time now

    $sql = "INSERT INTO wp99_rets_downloads(downloaddate,type) VALUES
('$starttime','1')";
    //need to get value not set it
    $id = $db->insert_sql($sql);
    if($id==false) {
        $db->insert_error("wp99_rets_errors","startdownload failed",null);
        die($db->last_error);
    } else {
        $i = updateall($rets,$date, $db, 0,1);
        if($id!=false) {
            //update last finish time
            $finishtime = date("Y-m-d H:i:s");
            $sql="UPDATE wp99_rets_downloads SET
endtime='".$finishtime."', updated='".$updated."',
inserted='".$inserted."', total='".$i."' WHERE downloadid=".$id;
            $db->update_sql($sql);
            echo "Updated " . $i . " Listings";
        }
    }
} else { die("could not find last date"); }



function updateall($rets,$modifydate,$db,$offset,$i) {
    global $updated;
    global $inserted;
    $limit = 4000;
    $select =
'Acres,AcresDesciption,AnnualMaintDesc,Appliances,Area,BathsFull,BathsHalf,BathsTotal,BedRoomDescription,BedsMax,BedsTotal,BuildingStories,Cable,City,Compactor,Connections,CoolSystem,Countertops,CountyOrParish,Defects,DepositPet,Description,DevelopCommunity,Disclosures,Dishwasher,Disposal,DOM,DwellingType,Efficiency,Electric,Energy,Exclusions,Exemptions,Exterior,FireplaceDescription,FireplacesNumber,FloorLocation,Floors,ForLease,ForSale,Foundation,GarageCarport,GarageDesc,Gas,GeoMarketArea,gpExt_Latitude,gpExt_Longitude,HeatSystem,HOAMandatory,Icemaker,Improvements,Interior,Latitude,Laundry,Legal,LegalSubdivision,ListAgentFullName,ListAgentMLSID,ListOfficeName,ListPrice,Location,Longitude,LotBackDimention,LotDesciption,LotDimention,LotFrontDimention,LotImprovements,LotLeftDimention,LotRighDimention,LotSize,MaintFeeAmt,MaintFeeIncludes,MasterBathDesc,MatrixModifiedDT,Matrix_Unique_ID,Microwave,MLSNumber,NewConstruction,NoOfGarageCap,OvenRange,OvenType,PetDepositDescription,Pets,PoolArea,PoolPrivate,PoolPrivateDesc,PostalCode,PropertyType,PublicRemarks,RangeType,Refrigerator,RentalTerms,RentalType,Restrictions,Roof,RoomCount,RoomDescription,SchoolDistrict,SchoolElementary,SchoolHigh,SchoolMiddle,Siding,SmokingAllowed,SqFtSource,SqFtTotal,StateOrProvince,Status,Stories,StreetName,StreetNumber,StreetNumberNumeric,Style,Subdivision,TaxAmount,Utilities,UtilityDistrict,WasherDryer,WaterSewer,YearBuilt';
    $query =
"(MatrixModifiedDT=$modifydate+),(Status=ACT,OP,PEND,PSHO),(PropertyType=RES,RNT,LND,CND,ACR)";
    $results = $rets->Search('Property', 'Listing', $query, ['Limit' =>
$limit, 'Offset' => $offset, 'Select' => $select]);
    $count = count($results);
    foreach ($results->toArray() as $listing) {
        $rows =
$db->insert_update_array('wp99_rets_property_listing',$listing);
        if($rows==1 ){
            //new record inserted
            $inserted++;
            $matrixid = $listing['Matrix_Unique_ID'];
            getrooms($rets,$db,$matrixid,"from new");
            getimages($rets,$db,$matrixid);
        } elseif($rows==2){
            $updated++;
            //echo $listing['Matrix_Unique_ID'] ." updated<br/>";
            //updating images and rooms
            deleterooms($db,$listing['Matrix_Unique_ID']);
            deleteimages($db,$listing['Matrix_Unique_ID']);
            getrooms($rets,$db, $listing['Matrix_Unique_ID'], "from
update");
            getimages($rets,$db,$listing['Matrix_Unique_ID']);
        //$db->insert_error("wp99_rets_errors", "Updated Row",
$listing['Matrix_Unique_ID']);
        } else {
            //echo "No Change<br/>";
        }
        $i++;
    }
    $offset = $offset + $limit;

    if($count<$limit)return $i;
    return updateall($rets,$modifydate,$db,$offset,$i,$updated,$inserted);
}
function deleterooms($db,$MatrixID) {
    //need to fix this to only update the ones who have changed...
    $where = "Listing_MUI = $MatrixID";
    $success = $db->delete("wp99_rets_property_rooms",$where);
    if($success==false) {
        $db->insert_error("wp99_rets_errors","delete room",$MatrixID);
    }
}
function deleteimages($db,$MatrixID){
    $where = "matrixID = $MatrixID";
    $success = $db->delete("wp99_rets_property_images",$where);
       if($success==false) {
        $db->insert_error("wp99_rets_errors","delete images",$MatrixID);
    }
}
function getrooms($rets, $db, $MatrixID, $sqltype) {
    $select =
'InputEntryOrder,Listing_MUI,matrix_unique_id,MatrixModifiedDT,RoomDimensions,RoomFeatures,RoomLocation,RoomType';
    $query = "(Listing_MUI=$MatrixID)";
    $results = $rets->Search('PropertySubTable', 'Room', $query,
['Limit' => 50, 'Select' => $select]);
    foreach ($results->toArray() as $room) {
        if ($db->insert_array('wp99_rets_property_rooms', $room) ==
false) {
$db->insert_error("wp99_rets_errors","room",$sqltype . "-" . $MatrixID);
        }
    }
}
function getimages($rets, $db, $MatrixID) {
//get images for property by Matrix_Unique_ID
    $objects = $rets->GetObject("Property", "LargePhoto", $MatrixID,
"*", 1);
    $i=0;
    foreach ($objects as $object) {
        // get the description of the object
        $des = addslashes($object->getContentDescription());

        // when requesting URLs, access the URL given back
        $img = $object->getLocation();

        // use the given URL and make it look like the RETS server gave
the object directly
        if($i==0) $main=1; else $main = 0;
        $sql = "INSERT INTO
wp99_rets_property_images(matrixID,url,description,main)VALUES($MatrixID,'$img','$des',$main)";
        //insert into datbase
        $i++;
        if($db->insert_sql($sql)==false) {
            //echo $db->print_last_error();
$db->insert_error("wp99_rets_errors","image",$MatrixID);
        } else {
            //echo "Inserted image $img <br/>";
        }
    }
}


Reply all
Reply to author
Forward
0 new messages