Hoppingvampireprogrammer Hoppingvampireprogrammer - 4 months ago 13
PHP Question

PHP "for" loop not looping properly

I have entered the "for" loop code below and it is not looping through all of my MYSQL data. Instead it loops through only the first row of data in the database (shown circled in red in this attached screenshot) and does NOT display the rest of the author/title/category/year/isbn info for the other 4 books in my database as it should.

I am using XAMPP 3.2.2 on a Windows 10 machine. If you need any more of my code for context, I can provide. Thanks!

for ($j = 0; $j <$rows; ++$j);
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);

echo <<<_END
<pre>
Author $row[0]
Title $row[1]
Category $row[2]
Year $row[3]
ISBN $row[4]
</pre>
<form action="sqltest.php" method="post">
<input type="hidden" name="delete" value="yes">
<input type="hidden" name="isbn" value="$row[4]">
<input type="submit" value="DELETE RECORD"></form>
_END;
}

Answer

This happens because your loop has an empty statement ;:

for ($j = 0; $j <$rows; ++$j); //<------ the semicolon !!

I'm sure this is just a typo ! Please remove the semicolon and try.