Question

I have a line of code to convert data from database to JSON. The only problem is that the code I have seems to be dropping the first record. Any thoughts to why.

$return_arr = Array();

$query_qrySelect = "SELECT * FROM table";
$qrySelect = mysql_query($query_qrySelect, $database) or die(mysql_error());
$row_qrySelect = mysql_fetch_assoc($qrySelect);
$totalRows_qrySelect = mysql_num_rows($qrySelect);

while ($row = mysql_fetch_array($qrySelect, MYSQL_ASSOC)) {
    array_push($return_arr,$row);
}
echo json_encode($return_arr);
Was it helpful?

Solution

This line:

$row_qrySelect = mysql_fetch_assoc($qrySelect);

is where you're "losing" your data.

OTHER TIPS

Because you call mysql_fetch_assoc and never use the result anywhere ($row_qrySelect is never used).

According to the manual:

Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top