tags:

views:

46

answers:

3

When I have something like this:

$row = mysqli_fetch_assoc($result);

Does $result now have one less row in it? If I loop mysqli_fetch_assoc through all the $result, will it be empty afterwards?

+2  A: 

$result is a resource pointer, it is not possible to add to, or remove data from it. It will give you a result and internally move on to the next record. The result as such is untouched.

The manual page provides additional info.

Pekka
+3  A: 

$result is a reference to a SQL result set, which contains all the rows returned in response to your query. Calling mysqli_fetch_assoc simply moves the result set's internal pointer further down the list of rows, eventually pointing to NULL (end of list). The rows are all still there, you haven't removed any of them: you've simply traversed the list, and you could run through them again by using something like mysqli_data_seek to reset the internal pointer to a specific row.

To clear $result, you'd need to call mysqli_free_result to "free" the result set.

Mike West
not quite agreeing with the "$result will still contain all the rows that were returned." Instead I would say that it's a reference to the resultset that the SQL has fetched.
thephpdeveloper
You're correct. I worded that poorly, which confuses the issue. I'll edit the answer to incorporate your note.
Mike West
+1  A: 

After the execution of that line, $result remains as a resource pointer, and can be used by other functions where applicable for further processing with $result as the reference to the database.

You can either unset it, or mysqli_free_result($result);

See: http://php.net/manual/en/mysqli-result.free.php

thephpdeveloper