Page 1 of 1

Comparing a text file to a DB

Posted: Tue Jul 16, 2002 11:08 am
by scrtagt69
I was wondering if this is possible: Compare a row from a table to a row from a text file, and if that row is not in that text file, then delete it from that table, and move it to another one. Say for example I read in a text file and assign the following variables:

Code: Select all

$fd = fopen ("d:\active.txt", "r");
while (!feof ($fd)) {
      $line= fgets($fd, 4096);

$MyArray=explode("|", $line);
$LastName=addslashes($MyArrayї0]);
$FirstName=addslashes($MyArrayї1]);
$MiddleName=$MyArrayї2];
$SSN=$MyArrayї3];

Then I have a table Active with the same fields:
LastName, FirstName, MiddleName, SSN

And an Inactive table exactly the same
LastName, FirstName, MiddleName, SSN

This text file is inserted into the Active table.

The reason is, is that this text file is updated every 2 weeks, and only current data should be maintained, with an archive for old data. So again, I want to move data from the Active table to the Inactive table which is not present in the text file. Can anyone give me an idea on how to do this??? Hope all this makes sense, if not let me know and ill try to clarify it more. Thanks

Posted: Tue Jul 16, 2002 12:27 pm
by hob_goblin
try something like

Code: Select all

$file = file('file.txt'); // creates an array of the file - putting each line as a key

$qry = mysql_query("SELECT * FROM table");

$i = 0;
while($data = mysql_fetch_array($qry)){
  if($fileї$i] != $data){
//delete $data
}
$i++;
}

Posted: Tue Jul 16, 2002 12:29 pm
by hob_goblin
oh, i didnt read the part about moving it, but you would just do a couple queries... maybe explode $file, and compare the keys