0

I'm trying to loop through a MySQL table using PHP, but it is only showing one line.

            //Retrieve a list of outstanding developments
            $sql = "SELECT * FROM tblDevelopment WHERE strStatus=?";
            $statement = mysqli_stmt_init($conn);

            //Check for any errors in the SQL statement
            if (!mysqli_stmt_prepare($statement,$sql)){

                //Report any errors with the prepared $statement
                header("Location: ../sqlerror.php");
                exit();

            } else {

                //If there are no errors, query the database for the username
                mysqli_stmt_bind_param($statement,'s', $status);
                mysqli_stmt_execute($statement);
                $results = mysqli_stmt_get_result($statement);

                if ($row = mysqli_fetch_assoc($results)) {

                    echo 'header';

                    while ($row = mysqli_fetch_assoc($results))
                    {
                         echo $row['strDetail'] . "</";
                    }

                    echo 'footer';

                } else {

                    echo 'No results to display';

                }

            }

The code works when there are no results, but it only shows one result when there are more than one - any ideas what I'm doing wrong?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Chris C
  • 3
  • 1
  • The if is grabbing one record. The then the while is grabbing another. Check [mysqli_num_rows](https://www.php.net/manual/en/mysqli-result.num-rows.php) to see how many records where affected. – Jason K Sep 12 '19 at 21:35
  • Thanks for answering - I've taken out the if bit, just using the while and it appears to be working perfectly. – Chris C Sep 12 '19 at 21:51

1 Answers1

0

You are almost there... you need to keep calling mysqli_fetch_assoc until you reach the end of the result set. Changing your if to a while should be enough to get you rolling.

while (($row = mysqli_fetch_assoc($results)) !== null) {

Alex Barker
  • 4,316
  • 4
  • 28
  • 47