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 4554498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:05:44+00:00 2026-05-21T17:05:44+00:00

i’ll try my best to explain the situation but i’m fairly new to php

  • 0

i’ll try my best to explain the situation but i’m fairly new to php and mysql so im a tad rusty.

What im specificity trying to achieve is a query resulting in the echo of “codes” that a customer has purchased.

My problem lies when a customer orders more then one item, each customer is given an order number (in the example: 1081) which links to a order_list db which lists which products they have purchased. So in this example 1081 ordered 3 products (1,11,14) this is shown in the database as..

od_id  |  pd_id
----------------
1081   |  1
1081   |  11
1081   |  14

So when i successfully query 1081 it brings back 3 rows of results. This is fine for this example and the quantity query but i only get one result back on the final sql query when i need all three results.

Here is my test code so far:

//Get order IDS
$sqlid = "SELECT pd_id FROM tbl_order_item WHERE od_id = 1081";
$result = mysql_query($sqlid);

if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sqlid;
die($message);
}

while($row = mysql_fetch_assoc($result)) {
$ids[] = $row['pd_id'];
}

foreach ($ids as $id) {
echo $id . "<br />";
}

//Get order Qtys
$sqlqty = "SELECT od_qty FROM tbl_order_item WHERE od_id = 1081";
$result = mysql_query($sqlqty);

if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sqlqty;
die($message);
}

while($row = mysql_fetch_assoc($result)) {
$qtys[] = $row['od_qty'];
}

foreach ($qtys as $qty) {
echo $qty . "<br />";
}

//Get Order Codes
$sqlcode = "SELECT od_code FROM tbl_order_code WHERE pd_id = $id LIMIT $qty";
$result = mysql_query($sqlcode);

if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sqlcode;
die($message);
}

while($row = mysql_fetch_assoc($result)) {
$codes[] = $row['od_code'];
} 

foreach ($codes as $code) {
echo $code . "<br />";
}

The current results on the page are this:

1   //Three results from "pd_id" (What the customer ordered)
11  //
14  //
1   //Three results from "od_qty" (How many the customer ordered
1   //
2   //
YYXY-YYXY-YYXY-YYXW   //The first code result with two codes as there was 2 qty for product 14
YYXY-YYXY-YYXY-YYXX   //

my test page you can view the above here.

I hope i made sense, basically i need the final “$sqlcode” query to process all the rows from the previous query not just the first.

Thanks!

Key:
pd_id //product id
tbl_order_item //ordered items table
od_id //order id 
od_qty //quantity of each product
od_code //order codes
tbl_order_code //codes table each code is related to the pd_id 
  • 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-21T17:05:45+00:00Added an answer on May 21, 2026 at 5:05 pm

    wrap the last sql in a foreach so that it gets executed for each $id

    you should really read up on sql joins, as could do this entire script as one $sql stmt

    foreach ($ids as $id) {
    
    $sqlcode = "SELECT od_code FROM tbl_order_code WHERE pd_id = $id LIMIT $qty";
    $result = mysql_query($sqlcode);
    
    if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $sqlcode;
    die($message);
    }
    
    while($row = mysql_fetch_assoc($result)) {
    $codes[] = $row['od_code'];
    } 
    
    }
    

    here is an attempt at doing it with a join (if that helps any)

    // query data
    $order_id = 1081;
    
    $sql = "SELECT tbl_order_item.pd_id, tbl_order_item.od_qty, tbl_order_code.od_code FROM tbl_order_item JOIN tbl_order_code ON 
    tbl_order_code.pd_id=tbl_order_item.pd_id
    WHERE tbl_order__item.od_id = $order_id";
    
    $result = mysql_query($sql);
    
    if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $sql;
    die($message);
    }
    
    
    while($row = mysql_fetch_assoc($result)) {
        printf("Order ID: %s - Product ID: %s - Qty: %s - Code: %s<br />", $order_id, $row['pd_id'], $row['od_qty'], $row['od_code']);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

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.