1

例如,屏幕上显示 5 条记录。我希望 $questionnum 显示数字 5,但它一直说 $questionnum 为 0。我怎样才能获得与屏幕上输出的行数实际匹配的行数。EG 如果 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>";
    }
4

2 回答 2

0

试试这个:

$questionnum = $stmt->num_rows;

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

$stmt->store_result();

阅读手册以获取更多信息。

于 2012-06-29T03:47:03.160 回答
0

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

$questionnum = sizeof($searchOption);
于 2012-06-29T04:08:41.550 回答