Omari Victor Omosa
Omari Victor Omosa

Reputation: 2879

While loops does not show all results php, it eliminates one result

Every time i use this code it eliminates one result.

Lets say if i have 10 rows from the database, it will only display 9 rows

I am aware this code is not in PDO or MySQLI, but that isn,t the issue

mysql_select_db($database_localhost, $localhost);
$query_tutorunit = "SELECT * FROM tutorunit WHERE tutorID = $colname_unitg";
$tutorunit = mysql_query($query_tutorunit, $localhost) or die(mysql_error());
$row_tutorunit = mysql_fetch_assoc($tutorunit);
$totalRows_tutorunit = mysql_num_rows($tutorunit);


$confirmtwo = "";
while ($row_tutorunit = mysql_fetch_array($tutorunit)) {
    $confirmtwo .= $row_tutorunit["code"];
}
echo $confirmtwo;

When i use do while it shows all the results.

<?php do { ?>
<p><?php echo $row_tutorunit["code"]; ?></p>
<?php } while ($row_tutorunit = mysql_fetch_assoc($tutorunit)); ?>

But i do not want to use do-while Where am i going wrong?

Upvotes: 0

Views: 73

Answers (1)

Yauheni Prakopchyk
Yauheni Prakopchyk

Reputation: 10912

Remove that line:

$row_tutorunit = mysql_fetch_assoc($tutorunit);

Both mysql_fetch_assoc and mysql_fetch_array move the pointer.

Upvotes: 1

Related Questions