0

我有一些使用 php 库 phprets 将当前属性同步到 MySQL 数据库的 php 代码。我在将记录放入其中时使用 REPLACE INTO 效果很好,我将 MLS 编号设置为 UNIQUE。问题是我必须更改它,因为我在事实之后向表中添加了带有两个额外列 lat 和 long 的地理编码,而 REPLACE 将删除地理编码。所以我改用 INSERT ON DUPLICATE UPDATE 语句。Not is 似乎没有插入新记录。更新似乎工作正常。

foreach ($property_classes as $class) {

    echo "+ Property:{$class}<br>\n";

    //[][=$file_name = strtolower("property_{$class}.csv");
    //$fh = fopen($file_name, "w+");

    $maxrows = true;
    $offset = 1;
    $limit = 1000;
    $fields_order = array();

    while ($maxrows) {

        $query = "(Status=S,A,P,B,H),({$rets_modtimestamp_field}={$previous_start_time}+)";
        // run RETS search
        echo "   + Query: {$query}  Limit: {$limit}  Offset: {$offset}<br>\n";
        $search = $rets->SearchQuery("Property", $class, $query, array('Limit' => $limit, 'Offset' => $offset, 'Format' => 'COMPACT-DECODED', 'Count' => 1));

        if ($rets->NumRows() > 0) {

            if ($offset == 1) {
                // print filename headers as first line
                $fields_order = $rets->SearchGetFields($search);
                //fputcsv($fh, $fields_order);
            }

            // process results
            while ($record = $rets->FetchRow($search)) {
                $this_record = array();
                foreach ($fields_order as $fo) {
                    $this_record[] = $record[$fo];
                }
                //fputcsv($fh, $this_record);
                $clean_records = str_replace('"', '', $this_record);
                $astring = implode('", "', $clean_records); 
                $astringTwo = '"'.$astring.'"';
                $fieldsstring = implode(",", $fields_order);
                $upArray = array_combine($fields_order,$clean_records);
                foreach ($upArray as $key => $value) {
                    $upArray[$key] = $key . "='" . $value."'";
                }
                $upStr=implode(", ", $upArray);
                $query="INSERT INTO CRMLS_property_residential ($fieldsstring) VALUES ($astringTwo) ON DUPLICATE KEY UPDATE $upStr";
                mysql_query($query);
                //echo $query;
            }

            $offset = ($offset + $rets->NumRows());

        }

        $maxrows = $rets->IsMaxrowsReached();
        echo "    + Total found: {$rets->TotalRecordsFound()}<br>\n";

        $rets->FreeResult($search);
    }

    //fclose($fh);

    echo "  - done<br>\n";

}
4

1 回答 1

0

这不能回答您的直接问题,但如果您改回 REPLACE INTO 流程,您可以从数据库中提取现有的纬度/经度值并将其放入数组中,这样您的流程的其余部分就好像它们确实来了从 RETS 服务器(而不是吹走您可能已经拥有的任何值)。这也让您有机会确定您是否已经拥有地理编码(如果没有,可以在当时完成并以相同的方式放置)。

于 2015-05-07T01:58:13.353 回答