简体   繁体   English

用phprets插入或更新mysql记录

[英]insert or update mysql records with phprets

I have some php code that uses the php library phprets to sync the current properties to MySQL database. 我有一些使用php库phprets将当前属性同步到MySQL数据库的php代码。 I was using REPLACE INTO when putting the records in which worked great, I have MLS number set to UNIQUE. 当我在工作良好的记录中使用REPLACE INTO时,我将MLS编号设置为UNIQUE。 The issue is that I had to change that because I am adding geocoding with two additional columns lat and long to the table after the fact and REPLACE would delete the geocoding. 问题是我必须更改它,因为事实之后,我将在表中添加另外两个字段lat和long的地理编码,而REPLACE会删除该地理编码。 So I changed over to a INSERT ON DUPLICATE UPDATE Statement. 因此,我切换到了INSERT ON DUPLICATE UPDATE语句。 Not is seems to be not be inserting new records. 好像不是要插入新记录。 The updating appears to be working fine. 更新似乎工作正常。

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";

}

This doesn't answer your direct question, but if you change back to the REPLACE INTO process, you could pull the existing lat/lon values from your database and put those into the array so the rest of your process acts as if they did come from the RETS server (rather than blowing away any values you might have already had there). 这并不能回答您的直接问题,但是如果您回到REPLACE INTO流程,则可以从数据库中提取现有的经纬度值并将其放入数组中,以便流程的其余部分就像它们来了一样从RETS服务器中获取(而不是散布您可能已经拥有的任何值)。 This also gives you the opportunity to determine whether or not you already have the geocodes (and if not, those could be done at that time and put in the same way). 这也使您有机会确定您是否已有地理编码(如果没有,则可以在那时进行,并以相同的方式放置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM