I
I
ivasenkoartem2019-06-30 20:55:39
SQL
ivasenkoartem, 2019-06-30 20:55:39

Why group by doesn't work?

Why does not it work?

<?php
  require_once 'connect_db.php';
  function getCategory() {
    global $mysqli;
    connectDB();
    $result = $mysqli->query("SELECT * FROM `shop` GROUP BY `category` ORDER BY `category` DESC");
    closeDB();
    return resultToArray5($result);
  }
  function resultToArray5($result){
    $array = array ();
    while (($row = $result->fetch_assoc()) != false)
      $array[] = $row;
    return $array;
  }
  print_r(getCategory());
 ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Leonid, 2019-06-30
@caballero

you need to specify the fields on which the aggregation is going on and not just an asterisk

B
bioGavs, 2019-06-30
@bioGavs

In the select section, you can specify fields that are in group by, or fields with aggregation functions, for example max(id).
Your query will work like this))
SELECT `category` FROM `shop` GROUP BY `category` ORDER BY `category` DESC
To make your query work, you can change the sql_mode setting with *, now you have it sql_mode=only_full_group_by. But it's not recommended

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question