简体   繁体   中英

PHP PDO fetch behaviour

Let's say the table table contains many records and I query a select statement with some conditions, in this case, foo=1 . After fetching a record, I'll use this record to do something which spends so much time, and I use sleep(2) to make it understand easily.

$stmt = $dbh->query('SELECT * FROM table WHERE foo=1');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    //do sth
    sleep(2);
}

After fetching the first row , if there is other session updating the table, for example, update the third row (let's assume the original foo of third row is 1) and set foo=0, will the above script still retrieve the third row ? I'm wondering what the fetch function does behind the code.

Generally when you execute a query it runs to completion, then the results are sent to your database driver, in this case PDO, for consumption.

Even when you're using techniques to get the data sequentially, like cursors or streaming rowsets, the effect is the same. The result of your query will not be impacted by subsequent statements.

Consider everything you retrieve from PDO to be a snapshot taken at a singular point in time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM