By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

SELECT COUNT for column in database?

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

Code goes as follows::
Expand|Select|Wrap|Line Numbers
  1.     <?php 
  2.          $sql = "SELECT * FROM input ORDER BY date DESC";
  3.          $result = $conn->query($sql);
  4.  
  5.          if ($result->num_rows > 0) { 
  6.          while($row = $result->fetch_assoc()) { 
  7.          $myid = $row["id"] ;
  8.      $sql3 = "SELECT COUNT question_id FROM output WHERE question_id = $myid";
  9.         $result3 = $conn->query($sql3);
  10.     ?>
  11.  
  12.     <div id="q">
  13.  
  14.             <small><p><?php echo $row["date"]; ?></p></small>
  15.             <p id="tag3"><small><?php echo $result3['']; ?></small></p>
  16.     </div>
  17.  
Any Suggestions will be appreciated..
Sep 6 '16 #1
Share this Question
Share on Google+
1 Reply


Expert Mod 100+
P: 589
You need to use a GROUP BY clause.
Expand|Select|Wrap|Line Numbers
  1. "SELECT question_id, COUNT(*) FROM output GROUP BY question_id"
Sep 6 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.