[英]SELECT COUNT for column in database?
如何計算表中與相關條件匹配的行數和計數的echo。
代碼如下::
<?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>
任何建議將不勝感激..
您需要使用COUNT(question_id)函數,如下所示
$sql3 = "SELECT COUNT(question_id ) as TotalQuestions FROM output WHERE question_id = $myid";
$result3 = $conn->query($sql3);
然后從上面的查詢中獲取結果使用fetch_fieled()
while ($info = $result3->fetch_field()) {
$TotalCount = $info->TotalQuestions ;
}
然后顯示值
<small><?php echo $TotalCount ; ?></small
報價遺失
$sql3 = "SELECT * question_id FROM output WHERE question_id = '".$myid."'";
其次,我沒有看到關閉括號
<?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(*) as rowCount FROM output WHERE question_id = '".$myid."'";
$result3 = $conn->query($sql3);
$rowCount= $result3->fetch_assoc();
?>
<div id="q">
<small><p><?php echo $row["date"]; ?></p></small>
<p id="tag3"><small><?php echo $rowCount['rowCount']; ?></small></p>
</div>
<?php }//while
} //if
?>
嘗試這個
$sql3 = "SELECT COUNT(1) as row_count FROM output WHERE question_id = ".$myid."";
$result3 = $conn->query($sql3);
$row_count = $result3->fetch_assoc();
echo $row_count['row_count'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.