Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3308020
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:28:25+00:00 2026-05-17T21:28:25+00:00

I have the following script which runs fine but I think a) it is

  • 0

I have the following script which runs fine but I think

a) it is ugly;

b) it doesn’t do what I need after the script is run.

Utlimately, the script should work as follows:

1) SQL selects all agents from a given table where the enrollment date is within the last month, grouped together by agent number where the count is greater than 1 ordered by enrollment date in ascending order. In short English, return a list of agents who have enrolled more than one member in the previous month.

2) Once the results are returned, the entire list is emailed to myself (not sure how to accomplish this yet) and the agent prize table is updated (agents enrolling more than one member get a prize).

To accomplish this, my script currently runs back to back foreach loops (which I hate) in order to break out the necessary field data.

Any suggestions for improving the process are appreciated.

SCRIPT:

<?php

    try {   
            $db = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $db->beginTransaction();

            $sql = $db->query("SELECT AGENT FROM tablename WHERE enroll_date > DATE_SUB(CURDATE(),INTERVAL 1 MONTH) GROUP BY AGENT HAVING COUNT(AGENT) >= 0 ORDER BY enroll_date ASC");             

            $result = $sql->fetchALL(PDO::FETCH_ASSOC);
            foreach($result as $key=>$val) {
                foreach($val as $ball=>$agentid) {

                    $sql2 = "UPDATE agent_prizes SET prize=?, method=? WHERE AGENT = '$agentid'";

                    $stmt = $db->prepare($sql2);
                    $stmt->execute(array("Y", "COMP"));


                    require('../phpmailer/class.phpmailer.php');

                    $mail             = new PHPMailer();
                    $mail->Username   = "user@domain.com";
                    $mail->Password   = "";
                    $mail->SetFrom("user@domain.com");
                    $mail->Subject    = "Subject";
                    $mail->MsgHTML("Message");

   //I NEED TO RUN YET ANOTHER QUERY SELECT ABOVE TO CAPTURE THE AGENT EMAIL ADDRESS, LETTING THEM KNOW THEY HAVE WON A PRIZE
                        $mail->AddAddress($row["EmailAddress"]);
                        $mail->Send();
                        $mail->ClearAttachments();
                        $mail->ClearAllRecipients();
                        $mail->ClearReplyTos();    
                    }
                }



            $db->commit();
            $db->null;
        }

    catch (PDOException $e)
        {
            $db->rollback();
            echo $e->getMessage();

            exit;
        }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:28:25+00:00Added an answer on May 17, 2026 at 9:28 pm

    This gets rid of the second foreach and (hopefully – I haven’t tested) gets the agent’s email address in the one select:

    ...
    $sql = $db->query("SELECT tablename.AGENT AGENT, agent.EMAIL EMAIL, 
                       FROM tablename, agent 
                       WHERE enroll_date > DATE_SUB(CURDATE(),INTERVAL 1 MONTH) AND tablename.AGENT = agent.AGENT 
                       GROUP BY tablename.AGENT 
                       HAVING COUNT(tablename.AGENT) > 1 
                       ORDER BY enroll_date ASC");             
    $result = $sql->fetchALL(PDO::FETCH_ASSOC);
    foreach($result as $row)
    {
      $sql2 = "UPDATE agent_prizes 
               SET prize=?, method=? 
               WHERE AGENT = '{$row["AGENT"]}'";
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lotus-script agent which runs as Web User since I need to
I have a script in kohana which runs fine on php 5.2.11. I took
I have the following script which runs once a day on cron on heroku.
I have a bash script which runs without needing to be root . But
I have the following PHP script which runs nicely and inserts all the data
I have a script which runs when the system boot first time. But that
I have the following script which first shows only the first para of the
I have the following script which appears multiple times on my page. I have
I have the following script which will delete a file off a BlackBerry. However,
Currently Im have the following script which checks to see if a checkbox value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.