-1

I'm learning PHP and MySQL and I have a question.

<?php
require('../connect_db.php');

$q='SELECT price FROM towels WHERE color = "Red"';
$r=mysqli_query($dbc, $q);

while($row=mysqli_fetch_array($r, MYSQLI_NUM)) {$var=$row[0];} 

In my case, I have towels with different prices that have color="Red". But it seems that the output in $var is only one of those prices.

It doesn't even seem to be always the largest or smallest price, or the price belonging to the first PrimaryKey in my table, or anything else that would seem intuitive.

so what is going on here?

Thanks.

2
  • Nothing, woops, i was gonna post another question first Commented Jul 10, 2015 at 10:14
  • 1
    Please go through the community before posting duplicate questions. This question is similar to stackoverflow.com/questions/17924387/… Commented Jul 10, 2015 at 10:20

1 Answer 1

0

For ordering you need to add following to the end:

ORDER BY price ASC

You are iterating throught your results in while. So every iteration $var gets the price of the next element. Depending on what you want to do with the prices you can list all of them in a seperate array. But as you didnt discribe what you want to do I can't exactly help you with that. If you edit that i can continue further on helping

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Ordering, yes, that is clear. When I echo $var after my "while" loop, I always get one specific price. How can I actually output all the prices that match my SELECT query?
echo the $var inside the while

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.