0

Why my while loop not working, I also have a while loop on the other PHP page, but there's only one page that doesn't work with PHP's while loop. But it does not contain any errors. Here's my code:

$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$num = mysqli_fetch_array($result);
if($num <= 0){
    echo "<h2>No records found.</h2>";
}
$x=0;
while($row = mysqli_fetch_assoc($result)){
    $x++;
    echo '
            <tr>
                <td>'.$x.'</td>
                <td>'.$row['permitno'].'</td>
                <td>'.$row['boarding_optr'].'</td>
                <td>'.$row['boarding_addr'].'</td>
                <td>'.$row['orno'].'</td>
                <td>'.$row['boarding_name'].'</td>
            </tr>
    ';
}
7
  • 2
    $num will be an array when you run $num = mysqli_fetch_array($result); So your following IF is not doing what you think Commented May 12, 2018 at 14:05
  • You will also have lost the first result in your result set as you run that before starting the while loop Commented May 12, 2018 at 14:08
  • 3
    You should use mysqli_num_rows instead of mysqli_fetch_array Commented May 12, 2018 at 14:10
  • As well as a more robust testing process before releasing code to LIVE Commented May 12, 2018 at 14:11
  • As for debugging in general, try dumping your variable before loops and conditions to check if they are as expected Commented May 12, 2018 at 14:15

2 Answers 2

2

You were reading your first result row and incorrectly using that as a count of resulted rows, then ignoring its content.

$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$num = mysqli_fetch_array($result);
// this reads the first row of your result set and then of course gets lost
//$num = mysqli_fetch_array($result);

// use mysqli_num_rows instead
if(mysqli_num_rows($result) <= 0){
      echo "<h2>No records found.</h2>";
} else {
    $x=0;

    // now this will get the first row, which you must have been missing before
    while($row = mysqli_fetch_assoc($result)){
         $x++;
         echo '
                <tr>
                    <td>'.$x.'</td>
                    <td>'.$row['permitno'].'</td>
                    <td>'.$row['boarding_optr'].'</td>
                    <td>'.$row['boarding_addr'].'</td>
                    <td>'.$row['orno'].'</td>
                    <td>'.$row['boarding_name'].'</td>
                </tr>
          ';
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank for your reply! I was not able to fix my problem lastnight because I was so sleepy. But then I realised that I messed up. I didn't see it coming :D haha. BTW big thanks!
0
$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$x=0;

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    $x++;
    echo '
      <tr>
      <td>'.$x.'</td>
      <td>'.$row['permitno'].'</td>
      <td>'.$row['boarding_optr'].'</td>
      <td>'.$row['boarding_addr'].'</td>
      <td>'.$row['orno'].'</td>
      <td>'.$row['boarding_name'].'</td>
      </tr>
    ';
  }
} 
else {
  echo "<h2>No records found.</h2>";
}

2 Comments

if $num is greater than 0 your while loop starts the same, try this
Add some description to help user to identify what was wrong and how your answer might help him/her.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.