php-mysqli_fetch_assoc-如果同时更改数据会怎样?

PHP中,我在while循环中使用mysqli_fetch_assoc()来获取特定查询中的所有记录.

我想知道如果在运行循环时(通过另一个进程或服务器)更改了数据,从而使记录不再与查询匹配,会发生什么情况.还会取吗?

换句话说,当您执行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?

是.

如果DBMS容易受到表更新和查询结果集迭代之间的竞争条件的影响,那将是不值得的.

当然,就数据库本身而言,在更改任何数据之前,您的SELECT查询已经完成.结果集将缓存在数据库和PHP脚本之间的层中的某个位置.

深入的

关于the ACID principle *:

In the context of databases, a single logical operation on the data is called a transaction.

用户启动的事务可以包含多个连续查询,但是ISO / IEC 9075-2中的4.33.4和4.33.5描述了如何在每个查询级别隐式进行查询:

The following SQL-statements are transaction-initiating
SQL-statements, i.e., if there is no current SQLtransaction, and an
SQL-statement of this class is executed, then an SQL-transaction is
initiated, usually before execution of that SQL-statement proceeds:

  • All SQL-schema statements
    • The following SQL-transaction statements:
    • <start transaction statement>.
    • <savepoint statement>.
    • <commit statement>.
    • <rollback statement>.
  • The following SQL-data statements:
    • [..]
    • <select statement: single row>.
    • <direct select statement: multiple rows>.
    • <dynamic single row select statement>.
    • [..]
  • [..]

另外,4.35.6:

Effects of SQL-statements in an SQL-transaction

The execution of an SQL-statement within an SQL-transaction has no
effect on SQL-data or schemas [..]. Together with serializable
execution, this implies that all read operations are repeatable
within an SQL-transaction
at isolation level SERIALIZABLE, except
for:

1) The effects of changes to SQL-data or schemas and its contents
made explicitly by the SQL-transaction itself.

2) The effects of differences in SQL parameter values supplied to externally-invoked
procedures.

3) The effects of references to time-varying system
variables such as CURRENT_DATE and CURRENT_USER.

您的广泛要求

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??

你不可以.

尽管可以控制连接器执行的缓冲类型(在本例中为MySQLi),但不能覆盖上述SQL的低级事实:INSERT或UPDATE或DELETE都不会影响正在进行的SELECT.

SELECT完成后,结果是独立的.您可以控制此独立数据的传输缓冲,但这并不能真正帮助您完成听起来像您想做的事情.

坦率地说,这很幸运,因为您想做的事情听起来很奇怪!

*严格来说,使用非默认存储引擎InnoDB,BDB和Cluster的表除外的表为MySQL has only partial ACID-compliance,而使用MyISAM does not support [user-instigated] transactions的表除外.否则,MyISAM本质上将毫无用处.

上一篇:即使所有内容都是utf-8编码的,为什么还要使用set_charset(“ utf8”)? (MySQLi-PHP)


下一篇:PHP-mysqli和bound语句,2031:语句中无参数数据