Error with this Email Script

This is a script that i made for my job and im having a problem. The email sends fine but it sends out the email 2 times for every contact. Any ideas why?


        //Set up this script to show errors for testing.

               // ini_set('display_errors', '1');

        //Define the Database Connection

                $conn =mysql_connect("IP ADDRESS","USERNAME","PASSWORD")
                                or die(mysql_error());
                 $db = mysql_select_db("PHP_Broadcast")
                                or die(mysql_error());

                $id = $_GET['id'];

                $sql3g = mysql_query("SELECT * FROM Messages WHERE id = '$id'");
                $row3g = mysql_fetch_array($sql3g);
                $group = $row3g['Group_to'];

        $sql = mysql_query("SELECT * FROM `Contacts` WHERE `Group_to` = '$group' AND Status = 'Active'");

           while($row = mysql_fetch_array($sql)) {   

      $sql4g = mysql_query("SELECT * FROM Messages WHERE id = '$id'");

                $row4g = mysql_fetch_array($sql4g);
                $subject = $row4g['Subject'];
                $message = $row4g['Message'];

        $First = $row['First'];
        $Last = $row['Last'];
        $Full_Name = "$First $Last";
        $City = $row['City'];
        $State = $row['State'];
        $Zip = $row['Zip'];
        $Phone = $row['Phone'];
        $Fax = $row['Fax'];
        $Userid = $row['userid'];
        $Email = $row['Email'];
        $soc = $row['SOCid'];

        $subject = str_replace("~~FIRST~~", $First, $subject);
        $subject = str_replace("~~LAST~~", $Last, $subject);
        $subject = str_replace("~~CITY~~", $City, $subject);
        $subject = str_replace("~~STATE~~", $State, $subject);
        $subject = str_replace("~~FULLNAME~~", $Full_Name, $subject);
        $subject = str_replace("~~EMAIL~~", $Email, $subject);

        $message = str_replace("~~STATE~~", $State, $message);
        $message = str_replace("~~FIRST~~", $First, $message);
        $message = str_replace("~~LAST~~", $Last, $message);
        $message = str_replace("~~CITY~~", $City, $message);
        $message = str_replace("~~EMAIL~~", $Email, $message);
        $message = str_replace("~~ZIP~~", $Zip, $message);
        $message = str_replace("~~FULLNAME~~", $Full_Name, $message);
        $message = str_replace("~~SOC~~", $soc, $message);

           $f_name = "MY NAME HERE";
           $f_email = "MY EMAIL HERE";

       	$headers = "From: ".$f_name." <".$f_email.">rn";
	$headers .= "To: ".$Full_Name." <".$Email.">rn";

        $to = $Email;
        $mailit = mail($to,$subject,$message,$headers);


                 echo "<center>($total) Message's Sent<br><br><a href="index.php">Go Home</a></center>";
		mysql_query("UPDATE Messages SET Sent='YES' WHERE id = '$id'");


It’s a tough little bug to discover, I must admit. It’s taken me quite a few months to finally realize what caused this (what seemed to me as an) inconsistent issue (this was during my early years as a PHP noob, mind you). What I found out later is that mysql_fetch_array returns two sets of the same results (one is associative, other other is numerical).

So which ones do i need to change, and what should i change them to? I would have NEVER known that if you didnt point it out.

Any suggestions?

You should remove the set you find less convenient in this case. Read up on mysql_fetch_array() in the PHP manual and see what I mean. This will allow you to make a choice based on that information, rather than ‘the guy on the other end of the internet’ ;)

Sponsor our Newsletter | Privacy Policy | Terms of Service