Answer the question
In order to leave comments, you need to log in
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
you need to specify the fields on which the aggregation is going on and not just an asterisk
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 questionAsk a Question
731 491 924 answers to any question