there is a table of files in the database Mysql field - id field - section

There are 5 files id = 1 section = maps id = 2 section = maps id = 3 section = weapons id = 4 section = weapons id = 5 section = weapons

on exit should get maps-2 weapons-3

My code for some reason does not work, gives an error, help me fix it, where did I make it ???

<?php require_once ($_SERVER["DOCUMENT_ROOT"]."/database.php"); $count = 0; $array = array(); $result = mysql_query("SELECT section FROM files WHERE game='1' GROUP BY section"); if(mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $array[$count] = $row; $count++; } $new_Array = array_count_values($array); foreach ($new_Array as $section=> $sum) { echo "$section ($sum)<br/>"; } } else { echo 'Файлов нет!'; } ?> 
  • > gives an error what? - IVsevolod
  • What error gives? The local telepaths are confused. - VenZell
  • Come on - telepaths see everything perfectly. $ array [$ count] = $ row ["section"]; But this is so that the code worked. And in order for it to fulfill its intended function by the author, it is necessary to correct the request. - Indifferent
  • > But this is so that the code worked and what problems did php have in putting $ row into the array $ array by index? problems further arise when we try to output an array of $ row (it is already $ sum) via echo. - IVsevolod
  • one
    array_count_values Generates an E_WARNING level error for each element that is not a string or an integer. And then no problems: $new_Array is an empty array. - Indifferent

3 answers 3

 $sql = "SELECT `section`, COUNT(*) AS `count` FROM `files` WHERE `game` = '1' GROUP BY `section`"; $result = mysql_query($sql); if (!$result || mysql_num_rows($result) == 0) { echo 'Файлов нет!'; } else { while ($r = mysql_fetch_assoc($result)) { echo $r["section"] . ' ' . $r["count"] . '<br>'; } } 
  • Thank you all, you helped me a lot!) - amf1k
 $sql = "SELECT section, count(1) FROM files WHERE game='1' GROUP BY section"; $data = $pdo->query($sql)->fetchAll(PDO::FETCH_KEY_PAIR); foreach ($data as $section => $count) { echo "$section ($count)"; } 

    SELECT section, count (section) FROM files WHERE game = '1' GROUP BY section

     while ($row = mysql_fetch_array($result)) { echo $row[0] , " - ", $row[1]; } 
    • Late: D - avengerweb
    • and how to output through the foreach loop? - amf1k