gigantorTRON Posted August 22, 2007 Share Posted August 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/66082-solved-comparing-arrays-help/ Share on other sites More sharing options...
Fadion Posted August 22, 2007 Share Posted August 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/66082-solved-comparing-arrays-help/#findComment-330509 Share on other sites More sharing options...
gigantorTRON Posted August 22, 2007 Author Share Posted August 22, 2007 That was just me being lazy and unfortunately it doesn't work... Quote Link to comment https://forums.phpfreaks.com/topic/66082-solved-comparing-arrays-help/#findComment-330511 Share on other sites More sharing options...
gigantorTRON Posted August 22, 2007 Author Share Posted August 22, 2007 Anymore help!??? Quote Link to comment https://forums.phpfreaks.com/topic/66082-solved-comparing-arrays-help/#findComment-330575 Share on other sites More sharing options...
jcanker Posted August 22, 2007 Share Posted August 22, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/66082-solved-comparing-arrays-help/#findComment-330592 Share on other sites More sharing options...
Hypnos Posted August 22, 2007 Share Posted August 22, 2007 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 } Quote Link to comment https://forums.phpfreaks.com/topic/66082-solved-comparing-arrays-help/#findComment-330599 Share on other sites More sharing options...
gigantorTRON Posted August 22, 2007 Author Share Posted August 22, 2007 Thanks! It looks like that's going to work. One more question... how would I pull out the values of 'fName' to insert during each iteration?? Quote Link to comment https://forums.phpfreaks.com/topic/66082-solved-comparing-arrays-help/#findComment-330669 Share on other sites More sharing options...
gigantorTRON Posted August 22, 2007 Author Share Posted August 22, 2007 any more help here? I need to get the values for the update query and I'm stuck. $cols[Rec[row$['id']]] something like that?? Quote Link to comment https://forums.phpfreaks.com/topic/66082-solved-comparing-arrays-help/#findComment-330910 Share on other sites More sharing options...
Hypnos Posted August 22, 2007 Share Posted August 22, 2007 $cols has 2 dimensions to it. When you're looping through the mysql while, you can read the old values like this: echo $cols[$row['id']]['Rec']; Quote Link to comment https://forums.phpfreaks.com/topic/66082-solved-comparing-arrays-help/#findComment-331086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.