0

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?

Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46
  • Which row is missing? First? Last? One from somewhere in the middle? – Mark Baker Mar 08 '16 at 21:59
  • 5
    Please show us your code around it. I can probably bet that you already have a line with `mysql_fetch_array($tutorunit)` above this code. – Rizier123 Mar 08 '16 at 21:59
  • I would guess you are doing a `mysql_fetch_array($tutorunit)` before you start the while loop – RiggsFolly Mar 08 '16 at 22:07
  • If you do not use numeric indexes, I advice to change `mysq_fetch_array` to `mysql_fetch_assoc` – barell Mar 08 '16 at 22:07
  • BUt when i use do-while it shows all the results. but i dont want to use the do while. this has always happened to me – Omari Victor Omosa Mar 08 '16 at 22:09
  • @barell even with that. it still eliminates one value – Omari Victor Omosa Mar 08 '16 at 22:10
  • 3
    *Called it!*, if the do-while works, this means somewhere above this code you already called `mysql_fetch_array($tutorunit)` otherwise the variable wouldn't be defined and you wouldn't see all correct results – Rizier123 Mar 08 '16 at 22:12
  • [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). – Rizier123 Mar 08 '16 at 22:12
  • @Rizier123 thank you i have seen it defined somewhere at the top an i have silenced it and it worked – Omari Victor Omosa Mar 08 '16 at 22:14
  • @Rizier123 I am aware of the mysql_* function. – Omari Victor Omosa Mar 08 '16 at 22:18

1 Answers1

1

Remove that line:

$row_tutorunit = mysql_fetch_assoc($tutorunit);

Both mysql_fetch_assoc and mysql_fetch_array move the pointer.

Yauheni Prakopchyk
  • 10,202
  • 4
  • 33
  • 37