SunQuest
           PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old July 16th, 2002, 06:06 PM
scrtagt69 scrtagt69 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 3 scrtagt69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
comparing data from text file to MySQL table

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:
$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

Reply With Quote
  #2  
Old July 16th, 2002, 07:06 PM
greggory greggory is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Reims, France
Posts: 82 greggory User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: comparing data from text file to MySQL table

My answers implies that you add a field named 'flag' in your 'active' table. It has to be a boolean, so a char is perfect.

The goal is to mark each record from the active table to know that it is in the file, then copy inactive records to that table, then delete inactive records from 'active'.

php Code:
Original - php Code
  1.  
  2. <?php
  3. // First mark that sthg is happening
  4. mysql_query( "update active set flag = 0" )
  5.      or die( mysql_error() );
  6.  
  7. $fd = fopen ("d:active.txt", "r");
  8. while (!feof ($fd)) {
  9.      $line= fgets($fd, 4096);
  10.  
  11.      $MyArray=explode("|", $line);
  12.      $LastName=addslashes($MyArray[0]);
  13.      $FirstName=addslashes($MyArray[1]);
  14.      $MiddleName=$MyArray[2];
  15.      $SSN=$MyArray[3];
  16.  
  17.  
  18.     // Then update each active line
  19.     // (assuming that two different people cannot have the same first & last name)
  20.     mysql_query( "update active set flag = 1 where lastname ='$LastName' and firstname = '$FirstName'" )
  21.       or die( mysql_error() );
  22. }
  23.  
  24. // We can now put records with 0 into the inactive table
  25. $r = mysql_query( "select lastname,firstname,middlename,ssh from active where flag = 0" )
  26.    or die( mysql_error() );
  27. while ( $tmp = mysql_fetch_array( $r )) {
  28.    mysql_query( "insert into inactive ( lastname,firstname,middlename,ssh ) values ( '".
  29.      addslashes( $tmp['lastname'] )."','".     
  30.      addslashes( $tmp['firstname'] )."','".     
  31.      addslashes( $tmp['middlename'] )."','".     
  32.      addslashes( $tmp['ssh'] )."' )" )
  33.    or die( mysql_error() );
  34. }
  35.  
  36. // Delete inactive
  37. mysql_query( "delete from active where flag = 0" )
  38.    or die( mysql_error() );
  39. ?>



Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > comparing data from text file to MySQL table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway