phppup Posted April 6, 2019 Share Posted April 6, 2019 I am UPGRADING some old files to MySQLi and while doing so have decided to complete some leftover projects. In this project, I was counting vegetables sold to customers that displayed a report displaying information like this: There were 3 orders for carrots for 1 lbs. each There were 2 orders for carrots for 2 lbs. each There were 1 orders for carrots for 3 lbs. each The code I used to generate the messages was: while($row = mysqli_fetch_array($result)){ echo "There were ". $row['COUNT(carrots)'] ." orders for carrots for ". $row['carrots'] ." lbs. each <br />"; } At the time, I was going to call a separate script for carrots, potatoes, and celery. Now, with my worldly advancement (or maybe not.. LOL) I realize I should be able to use the same single script and assign a variable to accommodate each vegetable item. After declaring the $item = 'carrots' I inserted it to test my theory. I tested the output in stages and all seemed okay until I did this: while($row = mysqli_fetch_array($result)){ echo "There were ". $row['COUNT($item)'] ." orders for carrots for ". $row['$item'] ." lbs. each <br />"; } Is there an issue with using a variable inside a $row? Is there a syntax error (no error messages)? Perhaps a MySQLi addition that is required? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 6, 2019 Share Posted April 6, 2019 What is your code for querying your data and producing the values you want to output? Quote Link to comment Share on other sites More sharing options...
phppup Posted April 6, 2019 Author Share Posted April 6, 2019 It was: $query = "SELECT carrots, COUNT(carrots) FROM $mytable WHERE carrots>0 GROUP BY carrots ORDER BY carrots ASC"; $result = mysqli_query($link, $query) or die(mysqli_error()); Until I changed all the CARROTS to $item. The loss of values did not occur until I altered the while statement. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 6, 2019 Share Posted April 6, 2019 Now it's even more confusing. What does your data look like? - it's hard to build a picture from what you've given us. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 6, 2019 Share Posted April 6, 2019 (edited) Note that within single quotes, PHP does not do substitution. echo "There were ". $row[COUNT($item)] ." orders for carrots for ". $row[$item] ." lbs. each <br />"; Edited April 6, 2019 by gw1500se Quote Link to comment Share on other sites More sharing options...
phppup Posted April 6, 2019 Author Share Posted April 6, 2019 The data is in $mytable input into a column named carrots. My output was There were 3 orders for carrots for 1 lbs. each There were 2 orders for carrots for 2 lbs. each There were 1 orders for carrots for 3 lbs. each but after altering the WHILE STATEMENT it became There were orders for carrots for lbs. each There were orders for carrots for lbs. each There were orders for carrots for lbs. each which removed the data information. Does something special or unique to MySQLi need to be done to change the hardcoded word "carrots" to the variable $item within that statement? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 6, 2019 Share Posted April 6, 2019 If you won't answer my question, why should anyone answer yours? Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 6, 2019 Share Posted April 6, 2019 Just what I would expect from the echo using those single quotes. Quote Link to comment Share on other sites More sharing options...
phppup Posted April 6, 2019 Author Share Posted April 6, 2019 The data is in a simple table with a column for carrots which received a numeric value of the amount that was ordered. The table is populated by a form submission. I think that is what you are asking me. I do not have access to my code right now, but will try altering the quotes suggested by gw1500se. Any additional guidance is welcome. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 6, 2019 Share Posted April 6, 2019 I think we would like to see your EXACT table definitions so we can see how you are storing things. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.