SELECT COUNT for column in database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harish3693
    New Member
    • Jun 2016
    • 1

    SELECT COUNT for column in database?

    How to Count number of rows in a table that matches to the related condition and echo that count out.

    Code goes as follows::
    Code:
        <?php 
             $sql = "SELECT * FROM input ORDER BY date DESC";
    		 $result = $conn->query($sql);
    								
    		 if ($result->num_rows > 0) { 
             while($row = $result->fetch_assoc()) { 
             $myid = $row["id"] ;
         $sql3 = "SELECT COUNT question_id FROM output WHERE question_id = $myid";
    		$result3 = $conn->query($sql3);
        ?>
    
        <div id="q">
                
    			<small><p><?php echo $row["date"]; ?></p></small>
    			<p id="tag3"><small><?php echo $result3['']; ?></small></p>
        </div>
    Any Suggestions will be appreciated..
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    You need to use a GROUP BY clause.
    Code:
    "SELECT question_id, COUNT(*) FROM output GROUP BY question_id"

    Comment

    Working...