我有一些使用 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";
}