0

For 10 rows in the query it only returns 8 rows but i get the fields right:

For Query data which returns less than 2 rows I get an error.

//create table to display all data
echo "<table border="1"> ";
echo "<tr>";
$row = mysqli_fetch_assoc($result);
    foreach($row as $key => $value) 
        {
            echo "<th>$key</th>";
        }
echo "</tr>";

while (($row = $result->fetch_assoc()) !== null) 
{
  $output = array();
  $i=1;

  echo "<tr>";

    foreach ($row as $columnName => $columnValue) 
        {
            $output[] = $columnName."=>". $columnValue;
            echo "<td>".$columnValue."</td>";
        }

  echo "</tr>";
}
echo "</table>";
14
  • 3
    Please post the foreach in its correct context of your fetch loop. I suspect there's something going on like calling mysqli_fetch_row() both inside and outside a loop, causing a row to be skipped since the result resource isn't rewound. Commented Nov 26, 2014 at 18:54
  • here is my query for 1 row of $result. I get no output. $row=mysqli_fetch_assoc($result); if (is_array($row)) { echo "Yes, this is an array<br>"; foreach($row as $key => $value) { echo"$key\n"; } } Commented Nov 26, 2014 at 18:58
  • the "invalid argument supplied for foreach()" part is probably due to not checking for mysqli_fetch_row() result against false which is returned instead of an array if there are no more rows to fetch Commented Nov 26, 2014 at 18:59
  • Do you mean to say that it skips the first two columns and only returns 8 columns ? The difference is significant from rows. Commented Nov 26, 2014 at 19:01
  • Adding my modified code in the original question where I am creating a table with 10 rows on data it only returns 8 Commented Nov 26, 2014 at 19:03

2 Answers 2

2

Edit: Thanks to @Michael Berkowski for his comments on the question.


Here is a modified version of your code that should work:

//create table to display all data
echo "<table border=1 >";
echo "<tr>";
//echo "<th> ## </th>";

$row = $result->fetch_assoc();  // stick to the object-oriented form. It is cleaner.
foreach ($row as $key => $value) 
{
    echo "<th>$key</th>";
}
echo "</tr>";

do
{
    $output = array();
    echo "<tr>";
    foreach ($row as $columnName => $columnValue) 
    {
        $output[$columnName] = $columnValue;  // this is neater.
        echo "<td>" . $columnValue . "</td>";
    }
    echo "</tr>";
} while ($row = $result->fetch_assoc());

echo "</table>";

You can use your first foreach() loop to print the keys and then use a do-while() loop to get your desired output.

Additional reading:

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

Comments

-1

You need to use

mysqli_fetch_assoc($result);

3 Comments

Are you kidding me? 2 upvotes? The OP isn't even using mysql_*().
It's now the correct API, but still not especially helpful. mysqli_fetch_row() returns a numeric indexed array and no matter which is used, the foreach should still return the correct number of columns (though the displayed output from echo "$key\n"; is less useful)
its not the columns but the number of rows that its skipping. The first two always. Please see the new code updated in the question

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.