简体   繁体   English

PHP PDO提取行为

[英]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 . 假设表表包含许多记录,并且我在某些情况下查询了一条select语句,在这种情况下为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. 提取记录后,我将使用该记录来执行花费大量时间的操作,并使用sleep(2)使其易于理解。

$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 ? 提取第一行后 ,如果还有其他会话更新表,例如,更新第三行 (假设第三行的原始foo为1)并设置foo = 0,则上述脚本是否仍会检索第三行 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. 通常,当您执行查询时,查询将运行到完成,然后将结果发送到数据库驱动程序(在本例中为PDO)以供使用。

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. 将您从PDO检索到的所有内容视为在单个时间点拍摄的快照。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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