1

I have a website with events, I got two important tables, one with all adresses for the first Newsletter and one for the registered users.

Now I want to send a Reminder to all adresses who doesn't register.

My idea is to compare the E-Mail Adresses from both tables (tbl_adresses and tbl_registered_usr) and "remove" all E-Mail Adresses which are in both tables.

My code looks like:

// Read records
$query = "SELECT email FROM wf_anlaesse WHERE anlaesseID = '$Datum'"; 
$query = mysql_query($query);

// Put them in array
for($i = 0; $array[$i] = mysql_fetch_assoc($query); $i++) ;

// Delete last empty one
array_pop($array);

print_r ($array);

echo "<br /><br /><br /><br />";

// Read records
$query2 = "SELECT email FROM wf_adressen"; 
$query2 = mysql_query($query2);

// Put them in array
for($i = 0; $array2[$i] = mysql_fetch_assoc($query2); $i++) ;

// Delete last empty one
array_pop($array2);

print_r ($array2);


echo "<br /><br /><br /><br />";    
$result = array_diff($array, $array2);
print_r($result);

My output is like this:

Array ( 
    [0] => Array ( [email] => E-MAIL ) 
    [1] => Array ( [email] => E-MAIL ) 
    [2] => Array ( [email] => E-MAIL ) 
    [3] => Array ( [email] => E-MAIL ) ... and so on


Array ( 
    [0] => Array ( [email] => E-MAIL ) 
    [1] => Array ( [email] => E-MAIL ) 
    [2] => Array ( [email] => E-MAIL ) 
    [3] => Array ( [email] => E-MAIL )`... and so on

Array ( )

The array_diff doesn't do the trick.

Any ideas?

Thanks! Chris

1
  • Be very careful when writing SQL interfacing code and be sure to read up on proper SQL escaping to avoid severe SQL injection bugs. As a note, mysql_query should not be used in new applications. It's a deprecated interface that's being removed from future versions of PHP. A modern replacement like PDO is not hard to learn and will make your database code easier to get right. Commented Jan 27, 2014 at 14:58

3 Answers 3

1

Looks to me like you want a left join, which gets all of the values in the first (left) table which match the condition. Using IS NULL you can specify values which aren't present in the second (right) table.

SELECT A.email
FROM wf_adressen A
LEFT JOIN wf_anlaesse B
ON A.email = B.email
WHERE B.email IS NULL

This will return all the emails in wf_adressen that do not appear in wf_anlaesse.

Here's an example using PostgreSQL:

CREATE TABLE wf_anlaesse (id SERIAL, email TEXT);
CREATE TABLE wf_adressen (id SERIAL, email TEXT, name TEXT);

INSERT INTO wf_adressen VALUES (DEFAULT, '[email protected]', 'Nik M');
INSERT INTO wf_adressen VALUES (DEFAULT, '[email protected]', 'Nigel J');

INSERT INTO wf_anlaesse VALUES (DEFAULT, '[email protected]'); -- only Nik's received it

SELECT A.email
FROM wf_adressen A
LEFT JOIN wf_anlaesse B
ON A.email = B.email
WHERE B.email IS NULL;

       email
-------------------
 [email protected]

While MySQL may or may not be different around creating the tables and inserting the values, I'm sure the join syntax is exactly the same.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Nik Thanks for the reply! Sorry for my late response. This solution works, but I need one tiny change. I need 2 WHERE-Clauses, WHERE B.email IS NULL AND B.anlaesseID = '$ID'. With this second WHERE it shows me nothing, it's blank. Isn't it possible to have Multiple WHERE-Clauses in an JOIN? Or where is my thinking problem? Thanks Chris
0

Replace

$result = array_diff($array, $array2);

with:

$result = array_intersect($array, $array2);

regards!

Comments

0
foreach($array as $val){
    foreach($array2 as $key => $val2){
    if($val["email"] == $val2["email"])
    unset($array2[$key]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.