Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Could someone please take a look and let me know what i'm doing wrong... What i'm trying to acheive is, in the Database i have:

AGENT_REF = 1
AGENT_REF = 2
AGENT_REF = 3
AGENT_REF = 4
AGENT_REF = 5

In a file uploaded that parses information

$blmarArray = array($agentref);

AGENT_REF = 1
AGENT_REF = 2
AGENT_REF = 3
AGENT_REF = 5

What i'd like to happen is to extract the AGENT_REF from the database that ISN'T in the file uploaded, in this case AGENT_REF = 4 so i can DELETE it from mysql.

$sql_archeck = mysql_query("SELECT `AGENT_REF` FROM `eprentals`");
$archeck = mysql_fetch_array($sql_archeck);
$sqlarArray = array($archeck);
$combyArrayDiff = array_diff($blmarArray, $sqlarArray);
print_r ($combyArrayDiff);

All i'm getting is the last or first AGENT REF from the database and not the ones that arn't present in the db. (In the db there is 11 but the file uploaded only 8 are present, so i'd like to delete (SHOW) the ones that are in the DB that have been removed from the file uploaded)

Hope you can offer me some guidance into where i'm wrong with this and I Thank you for your time and appriciate your help!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

from php manual:

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

so insted of array_diff($blmarArray, $sqlarArray) you should use array_diff($sqlarArray, $blmarArray)

= the big array as first parameter, and the samller array as 2nd parameter


you also need to fetch all rows from the database, not just the first

$query = "SELECT `AGENT_REF` FROM `eprentals`";
$resource = mysql_query($query);
$sqlarArray = array();
while($row = mysql_fetch_array($sql_archeck))
{
    $sqlarArray[] = $row['AGENT_REF'];
}
$combyArrayDiff = array_diff($sqlarArray, $blmarArray);

/* debug result */
echo "<p><b>SQL list:</b> " . implode(', ', $sqlarArray) . "</p>";
echo "<p><b>Uploaded list:</b> " . implode(', ', $blmarArray) . "</p>";
echo "<p><b>Diff list:</b> " . implode(', ', $combyArrayDiff ) . "</p>";
share|improve this answer
    
Hello, Thank you for your speedy responce, This is showing the first row in the db which is present in the file uploaded.. for eg it shows AGENT_REF = 1 and not AGENT_REF = 4 :) –  Steve Jul 19 '12 at 9:18
    
Just to show my results... :) Array ( [0] => Array ( [0] => 53384_3 [AGENT_REF] => 53384_3 ) ) Where it should be 53384_7 thats the difference :) The 53384_3 is the first one in the database –  Steve Jul 19 '12 at 9:21
    
you only fetch one row, you don't using a fetch loop –  Puggan Se Jul 19 '12 at 9:23
    
you mean add a while loop ???? –  Steve Jul 19 '12 at 9:29
    
yes, added exemple above –  Puggan Se Jul 19 '12 at 9:30
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.