Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post

    Compare arrays in MySQL using php?

    I have an array I am given on a irregular basis, say 3 - 5 times a week, and it is uploaded to a website via a form and inserted into a MySql table.

    I am needing to be able to compare the array I am given today to the table in MySQL and output the newest array and the difference on the same webpage.

    Example:

    I upload an array of current employees on Mon.
    On Wed. I am given the newest array to update the db with but I need to be able to distinguish which employees are in the db and not the newest array.. ie they were fired or quit. On Thurs. I need to do this again.

    The unique key is the Employee ID number so I am using REPLACE INTO in my php code.

    How can I accomplish this? I was thinking of a flag of some sort... but since it is a irregular update occurrence I cannot think of a way.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    How about fetching the data from the table into an array, then comparing that array to the new array to create your report. Then proceed onto the table update using the new array as it's always been done. There's an army of array functions in PHP to make it easy to compare two arrays.

    http://us.php.net/manual/en/function...diff-assoc.php

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post
    I was thinking about this however how do I query the db so array is in the same form? The one being submitted is 2 dimensional.

    Submitted array:

    PHP Code:
    Array (
                [
    1] => Array
            (
                                  [
    note] => 
                                  [
    careerID] => 20
                                  
    [lastLogin] => 1231125665
                                  
    [bearerStatus] => 0
                                  
    [lastLoginNumberHour24] => 22
                                  
    [dept] => 0
                                  
    [lastLoginNumberDay] => 4
                                  
    [statusNumber] => 1
                                  
    [lastLoginNumberMonth] => 1
                                  
    [lastLoginNumberYear] => 2009
                                  
    [memberID] => 28178

             

    When i query it I havent found anything on how to setup the numerical key for the outter array.
    Last edited by Hayyel; 03-16-2009 at 03:38 PM.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You'll have to get the two arrays to look the same somehow. Either convert the new array to use your unique primary key for the array index, or something.

    If that's too much of a pain, another method that would work is fetch through the existing table and perform an array search for each fetch. You'll need to keep track of array elements that you do find (probably by adding an index that indicates it's been found) so you can go back through the array and determine the ones that are new and should be inserted.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •