如何计算行数并显示该数字?

时间:2012-06-29 03:43:01

标签: php

让我们说例如屏幕上显示5条记录。我希望$ questionnum显示数字5,但它一直说$ questionnum是0.我怎么能得到实际与屏幕上输出的行数匹配的行数。 E.G如果5行则显示数字5,如果3行显示数字3等等

以下是代码:

 // Execute
    if (!$stmt->execute()) {
      die("Error executing statement: $stmt->error"); 
    }

    $stmt->store_result();

    // This will hold the search results
    $searchResults = array();
    $searchOption = array();

    // Fetch the results into an array
    if (!$stmt->num_rows()) {
      $stmt->bind_result($dbQuestionContent,$dbOptionType); 
      while ($stmt->fetch()) {
        $searchResults[] = $dbQuestionContent;
        $searchOption[] = $dbOptionType;
      }
    }

    if (isset($_GET['searchQuestion'])) {

      // If $terms is not empty we did a query
      if (!empty($terms)) {

          $questionnum = $stmt->num_rows;

        // If $searchResults is not empty we got results
        if (!empty($searchResults)) {
         echo"<p>Number of Questions Shown from the Search: <strong>$questionnum</strong></p>";
          echo "<table border='1' id='resulttbl'>
          foreach ($searchResults as $key=>$question) {
            echo '<tr class="questiontd"><td>'.htmlspecialchars($question).'</td>';
            echo '<td class="optiontypetd">'.htmlspecialchars($searchOption[$key]).'</td>';
    }
          echo "</table>";
    }

2 个答案:

答案 0 :(得分:0)

试试这个:

$questionnum = $stmt->num_rows;

如果它仍然是空的,可能是因为你忘了打电话:

$stmt->store_result();

阅读the manual了解详情。

答案 1 :(得分:0)

由于您已经将结果存储在数组中,只需执行

$questionnum = sizeof($searchOption);
相关问题