Jump to content

Recommended Posts

My program has 2 sets of data. One is the id column from a tab delimited file and it needs to be compared with the id column returned from a mySql query. The program needs to start with the data in the tab file id column and then compare ONE of the tab file ids to ALL of the ids in the user table. If a match is found, an Update query is run. If a match is not found, an Insert query is run.

 

$connection = mysql_connect("server","user","password");
mysql_select_db("myDB", $connection);
$result = mysql_query("SELECT id FROM user");

//tab delimited file
$fcontents = file('./myfile.txt.');

for ($i=0; $i<sizeof($fcontents); $i++)
{
     $line=trim($fcontents[$i]);
     $arr = explode("\t", $line);

     $cols = array('ID'=>$arr[0], 'Rec'=>$arr[1], 'fName'=>$arr[2], 'lName'=>$arr[3]);
     
      //Code here to check if the ids match
      while ($row=mysql_fetch_array($result) {
           if $cols['ID'] = $row['id'] 
              //Run UPDATE query on this $cols set
           else
             //Run INSERT query on this $cols set
}
}

 

I'm sure the answer is much easier than I think, but I'm having a hard time with this!!

 

I need some way to check the id value of the text file ($cols["ID"]) against the id value of the sql query results!

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/66082-solved-comparing-arrays-help/
Share on other sites

U wrote the if part just for illustrating or it is the actual code? As it should be:

 

if($cols['ID'] == $row['id']){
   //code
} else{
  //code
}

 

I guess it should work that way. Or also u can use in_array() but probably this isnt the case.

I had this question yesterday and posted a question about it.

 

I figured it out this morning, and posted my answer.  If you search back through the earlier posts from today, you'll see how I got it done.....

 

Mine was a bit more involved, but basically just put the two sets of data into an array and then step through the array and do if a != b statements

Your while loop is in a for loop. After the for loop finishes the first time, your mysql data set will be empty.

 

If ID is unique, I would use it for the array key. Then you don't need to do CPU intensive array searches.

 

Something line this:

<?php
//tab delimited file
$fcontents = file('./myfile.txt.');

for ($i=0; $i<sizeof($fcontents); $i++)
{
     $line=trim($fcontents[$i]);
     $arr = explode("\t", $line);
     $cols[$arr[0]] = array('ID'=>$arr[0], 'Rec'=>$arr[1], 'fName'=>$arr[2], 'lName'=>$arr[3]);
     
}

//Code here to check if the ids match
while ($row=mysql_fetch_array($result) {
if(isset($cols[$row['id']]) {

              //Run UPDATE query on this $cols set
}
    else {
             //Run INSERT query on this $cols set
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.