-2

So here I have a table of Products which contains ID, Name, Type & Price

Now I'm running a function to give me the minimal price of specific product types.

The datatype of Price is Price int(5,2)

function MinPrice($connection) {

    echo "Minimum price: <br>";


    $sql = "SELECT Type, min(Price)
            FROM Products
            GROUP BY Type;";

    $result = mysqli_query($connection,$sql);
    while ($row = mysqli_fetch_assoc($result)) {
      //Printing the result
      echo $row['Type'].'  ||  '.$row['Price'].'<br>';
    }
  }

The code works but not properly. I get the names of the product types but for the price, it gives me an error.

Undefined index: Price

Can anyone please help me out with this?

  • Does this answer your question? [How to get count of rows in MySQL table using PHP?](https://stackoverflow.com/questions/58227521/how-to-get-count-of-rows-in-mysql-table-using-php) – Dharman May 09 '20 at 22:33
  • Your SQL is returning the correct results, you are just not displaying them. See the link, it should help you understand few things. I would also recommend using `foreach` instead of `while` loop. – Dharman May 09 '20 at 22:34

1 Answers1

0

Try this:

$sql = "SELECT Type, min(Price) as Price
            FROM Products
            GROUP BY Type;";
OldProgrammer
  • 12,050
  • 4
  • 24
  • 45