Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on the desired behavior. By default, MYSQLI_STORE_RESULT is used.
If you use MYSQLI_USE_RESULT all subsequent calls will return error Commands out of sync unless you call mysqli_free_result().
下面又有人注释了这样一句:
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = mysqli_query($link, “SELECT * FROM City”, MYSQLI_USE_RESULT)) {
/* Note, that we can’t execute any functions which interact with the
server until result set was closed. All calls will return an
‘out of sync’ error */
if (!mysqli_query($link, “SET @a:=’this will not work’”)) {
printf(“Error: %s\n”, mysqli_error($link));
}
mysqli_free_result($result);
}
MYSQLI_USE_RESULT和MYSQLI_STORE_RESULT决定了mysqli client和server之间取结果集的方式。前者查询的时候并没有从server将结果集取回,后者查询时提取结果集返回给client,并分配内 存,存储到用户程序空间中,之后mysqli_fetch_array()相当于是从本地取数据;而MYSQLI_USE_RESULT方式 下,mysqli_fetch_array()每次都要向server请求结果行。
难怪phpmanual上那人注释说当检索大量数据时建议使用MYSQLI_USE_RESULT,因为MYSQLI_USE_RESULT有较低 的内存需求,而MYSQLI_STORE_RESULT需要在client本地维护结果集,内存开销大。说到这里,可能会 想,MYSQLI_USE_RESULT每次取数据时都要请求server,网络开销是不是要比MYSQLI_STORE_RESULT大呢?它节省的内 存开销与带来的网络开销占比究竟如何,还需具体的测试数据来分析。
参考资料 http://www.cnblogs.com/wangyonghui/archive/2013/04/24/3040824.html