0

I am doing an exercise with PHP and MySQL database management, and I'm trying to return an amount from a field in a table with PHP. I am trying to get the value from the CalsPerServ in the foods table where the foodsID is equal to the foodsID the user has selected. All I'm getting now though is an array, when I should be returning one result. Any idea what went wrong?

if($tableName == "meals"){
        $foodsID = filter_input(INPUT_POST, "foodsID");
        $servings = filter_input(INPUT_POST, "Servings");
        $foodsID = mysql_real_escape_string($foodsID);
        $servings = mysql_real_escape_string($servings);

        $query = "SELECT CalsPerServ FROM foods WHERE foodsID=$foodsID";
        $result = mysql_query($query, $connect);
        $CalsPerServ = mysql_fetch_assoc($result);

        print $CalsPerServ;

        $calories = ($CalsPerServ * $servings) * 100;

        $sql .= "(foodsID, Servings, Calories) VALUES ('$foodsID', '$servings', '$calories')";

        $sqltwo = "INSERT INTO daily_diary (foodsID, CaloriesPlus) VALUES ('$foodsID', '$calories')";
        $resulttwo = mysql_query($sqltwo, $connect);
}
6
  • 3
    mysql_fetch_assoc returns an associative array. And tell your teacher that mysql_* is deprecated. (php.net/manual/en/function.mysql-fetch-assoc.php)
    – teynon
    Commented Jun 17, 2013 at 22:59
  • 1
    -0.49 for using mysql_query in 2013. Take a look at mysqli or PDO. Either one supports prepared statements, which for one thing means you don't have to do all this manual escaping crap and cobbling together SQL by hand.
    – cHao
    Commented Jun 17, 2013 at 23:00
  • @Tom Tell me about it... I'm annoyed it's all he teaches! Commented Jun 17, 2013 at 23:06
  • 1
    +1 for learning process. You're a lot further than I am right now. And whoever minused 1 you, shame shame. Commented Jun 17, 2013 at 23:06
  • @cHao Really, trust me, I wouldn't be doing databases at all if this weren't for school! Commented Jun 17, 2013 at 23:06

2 Answers 2

1

The problem lies here in this line

$calories = ($CalsPerServ * $servings) * 100;

$CalsPerServ is an array containing the result from the query. In order to get single column value out of it you have to use it like this

$calories = ($CalsPerServ['CalsPerServ'] * $servings) * 100;
2
  • Ah! Thank you so much! I knew there was some little thing I wasn't doing. I guess my brain is fried after 8 hours of coding! Commented Jun 17, 2013 at 23:02
  • @RachelleBennington it happens mostly with the programmers :) Commented Jun 17, 2013 at 23:07
1

Even though you are querying for one thing, mysql_fetch_assoc() returns an array.

This way even if you have selected more than one thing, you will always know you will get back an associative array, with the selected items as its keys.

$CalsPerServ = mysql_fetch_assoc($result);

 print $CalsPerServ['CalsPerServ'];
1
  • Ah! Thank you so much! I knew there was some little thing I wasn't doing. I guess my brain is fried after 8 hours of coding! Commented Jun 17, 2013 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.