0

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);
1

2 Answers 2

2

This line:

$row_qrySelect = mysql_fetch_assoc($qrySelect);

is where you're "losing" your data.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.