为什么我只能从mysqli_fetch_assoc中获得一行?

时间:2019-04-15 21:39:41

标签: php mysql

在我的网站上,只有mysqli_fetch_assoc($ result)行。

$sql = "SELECT * FROM answers WHERE question_id = '$questionid' ORDER BY answer_id ASC";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
    echo "<div>";
    $answer = $row['answer'];
    $userid = $row['user_id'];
    $date = $row['answereddate'];
    echo '<section style="height: 200px;">';
    $username = $row['username'];
    echo '<div class="answer-container">
      <div id="description-container">';
    echo $answer;
    echo '</div>
      </div>
      <div class="profile-container">
      <a>';
    printDate($date);
    echo '<br>';
    echo '</a>
      </div>
      </section>';
    echo "</div><br>";
}

仅打印answer 1

如果我在phpmyadmin中执行$ sql,则会得到2行:

phpmyadmin

1 个答案:

答案 0 :(得分:-1)

为什么要使用数组?

$sql = "SELECT * FROM `answers` WHERE `question_id` = '12' ORDER BY `answer_id` ASC";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)) {
    $answer = $row['answer'];
    echo $answer;
}