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?

2
  • The if is grabbing one record. The then the while is grabbing another. Check mysqli_num_rows to see how many records where affected. Commented Sep 12, 2019 at 21:35
  • Thanks for answering - I've taken out the if bit, just using the while and it appears to be working perfectly. Commented Sep 12, 2019 at 21:51

1 Answer 1

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) {

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.