# Buffered Query
+ The results are sent from the `TiDB` server to `PHP` and kept in the `PHP` process's memory
+ Allows additional operations such as row counts and moving the pointer to the current result
+ Incurs a big memory overhead for large result sets
+ Use buffered queries ONLY for small result sets or if you need to know the number of rows before reading all rows
+ **`mysqli`**: enabled by default
  + To turn off: **`$res = mysqli->query($sql, MYSQLI_USE_RESULT)`**
+ **`PDO_MySQL`**: disabled by default
  + To turn on: **`$dbhandle->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true)`**

------------------------------------------------------------
For `mysqli`, `mysqli::store_result()` will fetch the whole resultset from the TiDB server while `mysqli::use_result()` will fetch the rows one by one.

For `PDO_MySQL`, set `PDO::MYSQL_ATTR_USE_BUFFERED_QUERY` to `true` or `false` to turn on or off the buffered query behavior.


Buffered queries are only suitable for small result sets or if you need to know the number of rows before reading all rows. 

If you anticipate a large number of rows and do not need to know in advance how many rows are in the result set, then turn off buffering in `mysqli` using the PHP statement shown in the slide. 
