Comparing a text file to a DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scrtagt69
Forum Newbie
Posts: 6
Joined: Thu Jun 13, 2002 11:56 am

Comparing a text file to a DB

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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++;
}
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
Post Reply