-3

enter image description hereIn my database table, there are many students from different classes. I want to get the number of students in each class in a table. How can I get this and insert into a table? Here is my code:

        <?php

        function showSearchResult()
        {
            require_once('config.php');
            connect_db(); 
            $class = '';
            $result = mysql_query("SELECT * FROM ipsc_student WHERE class=2");
            $num_rows = mysql_num_rows($result);
            $rt = "";
            $rt.="<div align='center'>
                        <h1>Report IPSC</h1>
                </div>";
            $rt.= "<table width='1000' align='center' border= '1'>";
            $rt.= "<tr><td><b>SL</b></td>
                    <td><b>Class</b></td>
                    <td><b>Total Student</b></td>
                    </tr>";
            for($i =1; $i < 13; $i++){
                $rt.="<tr>";
                $rt.="<td>$i.</td>";
                $rt.="<td>$i</td>";
                $rt.="<td>$num_rows</td>";
                $rt.="</tr>";
            } 
        echo $rt;
        }
        showSearchResult();
        ?>
2
  • select class, count(class) as 'total' from ipsc_student group by class ?? Commented Oct 27, 2016 at 6:33
  • use count: w3schools.com/sql/sql_func_count.asp Commented Oct 27, 2016 at 6:33

2 Answers 2

1

You can do group by class on your student table

SELECT `class`, COUNT(`student`) AS total_student FROM `ipsc_student` GROUP BY `class`
Sign up to request clarification or add additional context in comments.

Comments

0
insert into newtablename (columnname) select count(*) from      
oldtablename group by class

newtablename - the new table where u want to store the count columnname - column name in the new table oldtablename - thwe old table where want the count of students of each class

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.