The responses to this question do not give a clear answer and I cannot see how the chosen answer is correct because the question does not say anything about deleting all the rows in the table and there can be no need to TRUNCATE the table in order to replace the values in a row. So here is an answer albeit a bit late:
If you are using the replace() function where there is an id in the replace then it means that the record may exist in the first place otherwise you would always go straight to the insert() function to add a new record. Therefore there must be some field other than the primary key (id) that defines whether the record exists or not. In the above example lets assume that the title is the important field that defines whether a new record is needed or the fields in a record need to be updated.
I always to use the get_var() function with the replace() function as follows:
First check if the record exists (for the important field) so that the id can be passed on to the replace function as follows:
$recID = $wpdb->get_var( "SELECT id FROM ".($wpdb->prefix . 'fafa')." WHERE title LIKE ".$title."'");
Now use the replace function that will create a new record or update the fields for the $recID found:
$wpdb->replace( $wpdb->prefix . 'fafa',
array(
'id' => $recID,
'title' => trim($row->item(0)->nodeValue) ,
'liveprice' => trim($row->item(2)->nodeValue) ,
'changing' => trim($row->item(4)->nodeValue) ,
'lowest' => trim($row->item(6)->nodeValue) ,
'topest' => trim($row->item(8)->nodeValue) ,
'time' => trim($row->item(10)->nodeValue) ),
array(
'%d', '%s', '%s', '%s', '%s', '%s', '%s' )
);
Finally, after replace, the ID generated from the AUTO_INCREMENT column can be accessed using:
$wpdb->insert_id;
I hope that this helps someone who has a similar question to the original.