For some reason I’m having a problem retrieving data from my database. It leaves off the first item being listed.
Connection:
mysql_select_db($database_my_db, $my_db);
$query_rs_jobboards11 = "SELECT * FROM jobBoardsSpecTypes ORDER BY type";
$rs_jobboards11 = mysql_query($query_rs_jobboards11, $my_db) or die(mysql_error());
$row_rs_jobboards11 = mysql_fetch_assoc($rs_jobboards11);
$totalRows_rs_jobboards11 = mysql_num_rows($rs_jobboards11);
Output:
<form action="" method="get">
<select name="specialty">
<?php do { ?>
<option value="<?php echo $row_rs_cms11['type']; ?>"><?php echo $row_rs_cms11['type']; ?></option>
<?php } while ($row_rs_cms11 = mysql_fetch_assoc($rs_jobboards11)); ?>
</select>
<input type="submit" value="Go" />
</form>
It keeps leaving out the first entry in the type column. If I add more entries, then the one that was left out previously is shown, and the new entry is hidden.
What am I doing wrong? The database works perfectly fine for everything but this page.
You already assigned the first row to
$row_rs_jobboards11, so when you call$row_rs_cms11 = mysql_fetch_assoc($rs_jobboards11)in your do/while loop, it’s starting at the second row in the table.Using a do/while instead of a while doesn’t make any difference, but using different variable names does make a difference.
Update, to explain the difference between while, do/while, and, making sure your variable names are correct.
While loop, which works:
do/while loop, which also works
Note that the $row variable matches. If you leave out the first call, it will still work, but you’ll get an extra (empty) before any content is outputted. If you set the variable to a different name (and use a mysql_fetch_* call), the internal database pointer is set to the second row in the table. Consequently, you miss the first row in the table, because it’s set to a different (unused) variable.
Moral of the story:
You can use either a while or a do/while loop, you just have to make sure that