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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:28:03+00:00 2026-06-09T16:28:03+00:00

I got help for a previous problem/question, and just realized that the script I

  • 0

I got help for a previous problem/question, and just realized that the script I was given returns the “wrong” number/value (yet does not trigger php/mysql errors). Here’s the values:

ingredient #1: 20.216 calories

ingredient #2: 134.4564 calories

The script should return: 154.67 but instead returns: 91.35

The code:

<?php
//The Recipe selected
$recipe_id = $_POST['recipe_id'];

/*****************************************
 * Total CALORIES in this recipe
 *****************************************/

echo '<h4>Total calories in this recipe:</h4>';

$result = mysql_query(
  "SELECT SUM(ingredient_calories), ingredient_amount
  FROM recipe_ingredients
  INNER JOIN ingredients USING (ingredient_id)
  WHERE recipe_id = $recipe_id");

while ($row = mysql_fetch_array($result)) {
  $recipe_calories = (
    $row['SUM(ingredient_calories)']
    * $row['ingredient_amount']);
}
echo number_format($recipe_calories, 2);

mysql_free_result($result);
?>

The ingredients table, stores the default values and serving size for each ingredient.

The recipes table, stores/sets the recipe_id, and other data unrelated to the query)

The recipe_ingredients table, maps the ingredient(s) within a recipe and stores the amount(s) used in the recipe.

I’m sure it’s a problem with the query, but I too wet behind the ears to determine the problem. Any help is greatly appreciated 🙂

UPDATE: Here’s the table data, as requested

ingredients
-----------
ingredient_id
ingredient_name
ingredient_calories

recipes
-------
recipe_id
recipe_name

recipe_ingredients
------------------
recipe_id (fk)
ingredient_id (fk)
ingredient_amount
  • 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-09T16:28:04+00:00Added an answer on June 9, 2026 at 4:28 pm

    FIGURED IT OUT!

    Made the following adjustments:

    $result = mysql_query(
      "SELECT ingredient_calories, ingredient_amount
      FROM recipe_ingredients
      INNER JOIN ingredients USING (ingredient_id)
      WHERE recipe_id = $recipe_id");
    
    while ($row = mysql_fetch_array($result)) {
      $recipe_calories += (
        $row['ingredient_calories']
        * $row['ingredient_amount']);
    }
    echo number_format($recipe_calories, 2);
    

    Problem solved 🙂

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

Sidebar

Related Questions

I've got a problem with my previous question. Nobody can't help me so I'll
I just got help in how to compile this script a few mintues ago
This is a expansion on to a previous question that got answered yesterday, which
Thanks to the help in a previous question , I've got the following code
I've got a problem connected with my previous question: JList: sorting by Up/down buttons
So I have a tiny problem. I got help here a while ago about
Recently I got some help with grouping when a user enters a null value
I listed the follwoing question: http://stackoverflow.com/questions/11005335/python-sum-excel-file I got great help on it, and now
i got some problem and need help .. my plan : 1. get ip
I've got an app with a big 'help' page that describes how to use

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.