Forum Moderators: coopster
When I print the resulting array (useRow[]), the first row of results is not printed. I think maybe it's the 'while loop' as $totalRows does indicate the correct number of rows in the table and all the returned data in $row is correct. It's just missing a record.
(the table only has 5 rows and 4 columns)
$query= "SELECT * FROM myTable ORDER BY someField DESC";
//Connect to and query the database
$db=mysql_select_db($database_Con, $Con);
$results=mysql_query($query, $Con) or die(mysql_error());
$row=mysql_fetch_assoc($results);
$totalRows= mysql_num_rows($results);
//Get the results as an associative array and put each row into the numerically indexed array
$useRow=array();
while($row=mysql_fetch_assoc($results)){
$useRow[]=$row;
}
print_r($usedRow);
This seems pretty straight forward and should work. But I am stumped.
Any ideas?
Thanks.
Because you have this line:
$row=mysql_fetch_assoc($results);
The internal pointer is already advanced once, so the next time you call it within your while loop, it will start at the second row. You can achieve the same thing by elimination this line:
$query= "SELECT * FROM myTable ORDER BY someField DESC";
//Connect to and query the database
$db=mysql_select_db($database_Con, $Con);
$results=mysql_query($query, $Con) or die(mysql_error());
$totalRows= mysql_num_rows($results);
//Get the results as an associative array and put each row into the numerically indexed array
$useRow=array();
while($row=mysql_fetch_assoc($results)){
$useRow[]=$row;
}
echo '<pre>';
print_r($usedRow);
echo '</pre>';
Good luck! :)
Glad you got everything sorted!