In PHP I’m using mysqli_fetch_assoc() in a while-loop to get every record in a certain query.
I’m wondering what happens if the data is changed while running the loop (by another process or server), so that the record doesn’t match the query any more. Will it still be fetched?
In other words, is the array of records that are fetched fixed, when you do query()? Or is it not?
Update:
I understand that it’s a feature that the resultset is not changed when the data is changed, but what if you actually WANT that? In my loop I’m not interested in records that are already updated by another server. How do I check for that, without doing a new query for each record that I fetch??
UPDATE:
Detailed explanation:
I’m working on some kind of searchengine-scraper that searches for values in a database. This is done by a few servers at the same time. Items that have been scraped shouldn’t be searched anymore. I can’t really control which server searches which item, I was hoping I could check the status of an item, while fetching the recordset. Since it’s a big dataset, I don’t transfer the entire resultset before searching, I fetch each record when I need it…
Introduction
Yes.
Yes.
A DBMS would not be worth its salt were it vulnerable to race conditions between table updates and query resultset iteration.
Certainly, as far as the database itself is concerned, your
SELECTquery has completed before any data can be changed; the resultset is cached somewhere in the layers between your database and your PHP script.In-depth
With respect to the ACID principle *:
User-instigated
TRANSACTIONs can encompass several consecutive queries, but 4.33.4 and 4.33.5 in ISO/IEC 9075-2 describe how this takes place implicitly on the per-query level:In addition, 4.35.6:
Your wider requirement
You may not.
Although you can control the type of buffering performed by your connector (in this case, MySQLi), you cannot override the above-explained low-level fact of SQL: no
INSERTorUPDATEorDELETEwill have an effect on aSELECTin progress.Once the
SELECThas completed, the results are independent; it is the buffering of transport of this independent data that you can control, but that doesn’t really help you to do what it sounds like you want to do.This is rather fortunate, frankly, because what you want to do sounds rather bizarre!
* Strictly speaking, MySQL has only partial ACID-compliance for tables other than those with the non-default storage engines InnoDB, BDB and Cluster, and MyISAM does not support [user-instigated] transactions. Still, it seems like the “I” should remain applicable here; MyISAM would be essentially useless otherwise.