1

This section of code should output the data of all the answer provided to that question.

This code just freezes when it is run. I've tried changing the function with no prevail.

$sql2 = "SELECT answer.a_id, answer.u_id, answer.answer, answer.a_datetime, user.user_id, user.username FROM answer LEFT JOIN user ON answer.u_id = user.user_id WHERE q_id='$pid'";
$result2 = mysqli_query($db, $sql2);
$rows = mysqli_fetch_assoc($result2);
while($rows) {
        echo '<table border=2>';
        echo '<tr>';
                echo '<td>Answered by: '.$rows['username'].'</td>';
                echo '</tr>';
            echo '<tr>';
                echo '<td>'.$rows['answer'].'</td>';
            echo '</tr>';
            echo '<tr>';
                echo '<td>'.$rows['a_datetime'].'</td>';
            echo '</tr>';
        echo '</table>';
    }
  • Did you got any `error` ? What is not working ? This `code just freezes..` doesn't give any information about problem. Try to [debug](https://stackoverflow.com/a/21429652/10606400) your code and add errors in above question . – Swati Sep 07 '19 at 15:41
  • @Swati After executing the code, my laptop would max out its CPU usage and ultimately freezes out. I have to hard shutdown in order to start it again. – Thenepaligamer Sep 08 '19 at 08:46

2 Answers2

1

In your loop...

while($rows) {

as you don't change the value in the loop - it will always be the value it started with, so will never terminate, think you want to use...

$result2 = mysqli_query($db, $sql2);
while($rows = mysqli_fetch_assoc($result2)) {

which will fetch each row in turn and display the output before moving onto the next row.

You may also want to move the table definition outside of the loop, unless you want each row to be in it's own table.

Nigel Ren
  • 51,875
  • 11
  • 34
  • 49
0

Nigel does provide a solution, but lets go over the other glaring issues. I've re-written your code, making two major changes, you still may need to do some tweaking as I'm not familiar with your db or other named variables.

1 - Prepared Statement

How it helps and what is does Instead of re-answering this for the hundredth time, other people do a much better job of explaining it:

How can prepared statements protect from SQL injection attacks?

Helper function - You'll want this after you get a better understanding of how prepared statements work.

https://phpdelusions.net/mysqli/simple

2 - OOP

I rewrote this in the Object-Oriented syntax. I find it a lot less verbose and usually a bit easier to understand. What you win with the syntax change you loose with doing prepared statements as generally they involve more steps (but for the greater good)

$sql2 = "SELECT a_id, u_id, answer, a_datetime, user_id, username FROM answer LEFT JOIN user ON answer.u_id = user.user_id WHERE q_id = ?";
$stmt -> $con -> prepare($sql2);
$stmt -> bind_param("s", $pid);
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($aid, $uid, $answer, $date, $userid, $username);

echo '<table border=2>';

while($stmt -> fetch()) {
      echo '<tr>';
        echo '<td>Answered by: '. $username .'</td>';
      echo '</tr>';
      echo '<tr>';
        echo '<td>'. $answer .'</td>';
      echo '</tr>';
      echo '<tr>';
        echo '<td>'. $date .'</td>';
      echo '</tr>';
    }

 echo '</table>';

$stmt -> free_result();
$stmt -> close();
$con -> close();


While we're picking hairs, you should try not to echo so much HTML. You can close php inside a while loop and it will work the same. I fixed some of the HTML issues you have as well. (Should be using <tbody>, and just for fun I threw a <thead> in there too

// after bind result in the code above
?>
<table>
  <thead>
    <th>Answered by</th>
    <th>Answer</th>
    <th>Date</th>
  </thead>
  <tbody>
    <?php
    while($stmt -> fetch()) {
    ?>
    <tr>
      <td><?php echo $username;?></td>
    </tr>
    <tr>
      <td><?php echo $answer;?></td>
    </tr>
    <tr>
      <td><?php echo $date;?></td>
    </tr>
    <?php
    }
    ?>
  </tbody>
</table>

sBucholtz
  • 74
  • 9
  • *you should try not to echo so much HTML*, some people would say the opposite and prefer not to switch between HTML and PHP modes so much as it can be confusing at times. Although ultimately it is better to move to templates so that the code is completely separate - but beyond the scope of this question. – Nigel Ren Sep 08 '19 at 07:55