Jump to content

upgrade OR implimentation problem


phppup

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.