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

  • SEARCH
  • Home
  • 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 7877451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:22:31+00:00 2026-06-03T03:22:31+00:00

I have a query which is searching a database for orders. My SQL query

  • 0

I have a query which is searching a database for orders. My SQL query returns order information with the order id being the key (the id is repeating for orders with more than one product) and order information attached to that including product qty and variation.

The query joins a few tables and outputs as follows

|--orderid--|--orderstatus--|--ordcustid--|--orderprodname--|--orderprodqty--|--orderqty--|
|---12635---|-----11--------|-----4192----|----Product1-----|--------1-------|-----2------|
|---12635---|-----11--------|-----4192----|----Product2-----|--------1-------|-----2------|
|---12636---|-----11--------|-----3531----|----Product3-----|--------1-------|-----1------|

As you can see above. Customer 4192 has made 1 order (orderid 12635) that has 2 products. Customer 3531 has made 1 order that has 1 product (orderid 12636).

I’m wanting to capture the information for each order and put it together. So while the order id is the same I’m collecting the information.

My current script looks like this, but it just works on every row.

$result = mysql_query(" 
    SELECT * 
    FROM  orders, orderproducts, productimage, customers
    WHERE orders.orderid = orderproducts.orderorderid
    AND orderproducts.orderprodid = productimage.imageprodid
    AND orders.ordcustid = customers.customerid
    AND ordstatus = '11'
    ORDER BY orderid
");

while($row = mysql_fetch_array($result))
  {
//do stuff
}

The purpose of the script is to mail the information for each order. What my current script is doing is mailing the customer for every product (i.e. customer 4192 would get two separate emails – 1 for Product1 and 1 for Product2). What I’m wanting to do is email customer 4192 with both products on the same email.

How can I improve my script so that while the orderid is the same it will get information from all of the rows where the orderid is the same (eg orderproductname, qty ) and then continue when the order id changes.

  • 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-06-03T03:22:32+00:00Added an answer on June 3, 2026 at 3:22 am

    Try adding this bit of code before your loop:

     $last_id = 0;
    

    And in your while() loop:

     if ( $last_id != $row['orderid'] ) {
          // New Order - do some initiation stuff
          $last_id = $row['orderid'];
     } else {
          // Continuing the current order - do some different stuff
     }
    

    Here’s the code altogether:

    $result = mysql_query(" 
        SELECT * 
        FROM  orders, orderproducts, productimage, customers
        WHERE orders.orderid = orderproducts.orderorderid
        AND orderproducts.orderprodid = productimage.imageprodid
        AND orders.ordcustid = customers.customerid
        AND ordstatus = '11'
        ORDER BY orderid
    ");
    
    $last_id = 0;
    while($row = mysql_fetch_array($result)) {
        if ( $last_id != $row['orderid'] ){
            // Initiate a new order
    
            $message_body = ''; // Reset message body for each order
            $last_id = $row['orderid']; // Keep track of the orderid last used
            if ( $last_id > 0 ){
                // Make certain we didn't just begin the loop, then email the customer
    
                echo "SAMPLE MESSAGE BODY FOR ORDER $last_id <br/>\r\n";
                echo $message_body;
    
                // Uncomment following lines to send an email
                // $headers = $headers = 'From: My Store <info@mystore.com>\r\nContent-Type: text/html; charset=ISO-8859-1\r\n";
                // $success = mail( 'customer@email.com', 'Order Confirmation', $message_body, $headers);
    
            }
        } else {
            // Step through current order
    
            // Add a line to the message body for each product record
            $message_body .= "Product {$row['orderprodname']}, Qty: {$row['orderprodqty']} <br/>\r\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql/php query which searches through a database and returns matches based
I have a query which pulls a count from a DB by searching by
I have following query which has select query that returns data in 5sec. But
I have a query which produces a list of orders like this: Invoice Description
I have this query which works correctly in MySQL. More background on it here
I have a query which returns a series of cells of data from a
I have written an sql query to search 2 columns of a database, partNo
I have a SQL Server 2005 database in which I have some tables contain
I have this query which is running perfectly From this query I am selecting
I have a query which is meant to show me any rows in table

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.