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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:42:25+00:00 2026-05-30T03:42:25+00:00

<?php $transactionOutput = ; $sql = mysql_query(SELECT * FROM transactions WHERE emailaddress=’$email’); $productCount =

  • 0
<?php
$transactionOutput = "";
$sql = mysql_query("SELECT * FROM transactions WHERE emailaddress='$email'");
$productCount = mysql_num_rows($sql);
if($productCount > 0) {
while($row = mysql_fetch_array($sql)) {
    $item_id = $row["item_id"];
    $quantity = $row["quantity"];
    $size = $row["size"];
    $price = $row["price"]; 

    $sql = mysql_query("SELECT * FROM products WHERE id='$item_id'");
    $productCount = mysql_num_rows($sql);
    while($row = mysql_fetch_array($sql)) {
    $product_name = $row["product_name"];
    }

    $transactionOutput .= "<tr>";
    $transactionOutput .= "<td align='center'>" .$product_name. "</td>";
    $transactionOutput .= "<td align='center'>" .$quantity. "</td>";    
    $transactionOutput .= "<td align='center'>" .$size. "</td>";
    $transactionOutput .= "<td align='center'>" .$price. "</td>";
    $transactionOutput .= "</tr>";
}

    } else {
$transaction_list = "You have made no transactions yet";
}


?>

I’m trying to access data from two different tables and then return the product name of each item by matching the id in the product table with the item_id returned from the transactions table. This does output the correct information however it only shows the first transaction and no others, i know this is also probably horribly programmed too

  • 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-30T03:42:26+00:00Added an answer on May 30, 2026 at 3:42 am

    The problem seems to be that you are using the same variable $sql to store the resultsets from both the queries. So what might be happening here is this:

    1. the transaction query runs and the resultset is stored in $sql
    2. first record is read from the transaction resultset
    3. for the first transaction record, the product query runs and the resultset is stored again in $sql
    4. the output is stored into $transactionOutput
    5. next record is read from the transaction resultset

    AND the step 5 is the problem because the original transaction resultset – $sql – was overwritten by the product resultset.

    Try using another variable for the product query:

    $rsProduct = mysql_query("SELECT * FROM products WHERE id='$item_id'");
        $productCount = mysql_num_rows($rsProduct);
        while($row = mysql_fetch_array($rsProduct)) {
        $product_name = $row["product_name"];
    }
    

    Hope the above makes sense!

    EDIT: as an additional suggestion, you may like to try using JOIN queries to retrieve both transaction and product in the same query. Here:

    SELECT `t`.*, `p`.`product_name`
    FROM `transactions` `t`
    LEFT JOIN `products` `p` ON `t`.`item_id` = `p`.`id`
    WHERE `t`.`emailaddress` = '$email';
    

    Just loop the resultset and you are done!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP: Code: $category_searchresult = $db->db_query(SELECT category_code, category_name, category_comment FROM qa_categories WHERE .$search_by. LIKE '%.$search_string.%');
php gives the ability to send arrays from the _GET. example: test.php?var1=abc&arr[0]=1&arr[3]=test will output:
PHP has a great function called htmlspecialcharacters() where you pass it a string and
PHP's explode function returns an array of strings split on some provided substring. It
PHP, as we all know is very loosely typed. The language does not require
PHP stores its session information on the file system of the host of the
PHP treats all arrays as associative, so there aren't any built in functions. Can
PHP has a number of opcode caches, which as i understand it are scripts
PHP 4.4 and PHP 5.2.3 under Apache 2.2.4 on ubuntu. I am running Moodle
PHP has a very nice function, isset($variableName). It checks if $variableName is already defined

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.