View Full Version : Send mail to DB users with link

Patrik Iden
01-25-2012, 10:19 PM
I have i DB a table named "jobadd". And in this table i have the columns "Kategori" and "Länk"
in "Kategori" i have different add categorys ex, "bygg" and in "Länk" i have links that will direct a user
to a page so that the user can see the details of this add.

And in the table USERS i have the columns "Kategori" and "email" in "Kategory" i have different categorys that a company
picks when registering on the site ex, "Bygg & Anläggning" and in "email" is the email to this company.

Now what i want is to send a mail to the companys that is in the "USERS" / "Bygg & Anläggning" categoty. After checking
in the "jobadd" table if there are some new (24 hrs) adds in the category "bygg" and if there are, then send a mail containing the
link(Länk) to this add.

I'v got this code but it only sends the top link(Länk) in DB to all the emails in DB. But i also need it not to send the same link(Länk) two times.


$link=mysql_connect($mysqladd, $mysqluser, $mysqlpass, true) or die('Could not connect to database: ' . mysql_error());

mysql_select_db($databasename, $link) or die('Could not connect to table: ' . mysql_error());

$query="SELECT *, DATE_FORMAT(Time_of_Submission, '%Y/%m/%d') FROM jobadd WHERE Kategori='bygg' AND Time_of_Submission BETWEEN SYSDATE() - INTERVAL 1 DAY AND SYSDATE() ORDER BY ID DESC";
$query2="SELECT * FROM USERS WHERE Kategori='Bygg & Anläggning'";



$mailmessage = mysql_fetch_array($result);

$mailmessage2 = mysql_fetch_array($result2);



if (!mysql_query($query,$link))
die('Error: ' . mysql_error());

while ($i < $num) {


// Your email address

$fromemail = "no_reply@jobler.se";

// The subject

$subject = "Det har kommit in en ny Tjänstförfrågan på Jobler.se som passar er profil (klicka på länken nedan)";

// The message

$message = $mailmessage['where']." ".$mailmessage['Länk']."\n";

mail($email, $subject, $message, "From: $fromemail");

echo "Förfrågan skickad";


while ($i < $num) {


echo "<b><br>$where</b><br>$submitted<hr><br>";
echo "<b><br>$submitted2</b><hr><br>";