📜 ⬆️ ⬇️

mysqlnd

mysqlnd is a PHP extension that is the default MySQL driver for PHP 5.4. It works directly with the MySQL server, which means that the MySQL client, as well as the overhead projector for working with it, is no longer required!

image


')
mysqli_fetch_all


Despite the fact that the API for working with MySQL has not changed (the mysqli and PDO extensions), new functions have appeared in the mysqli extension, among which is the mysqli_fetch_all function.

Previously, to get the full result of the sample, we had to write a similar construction:

$result = mysqli_query($link, $query); $data = array(); while($row = mysqli_fetch_assoc($result)) { $data[] = $row; } 


In the case of using mysqli_fetch_all, the use of such constructions can be avoided:

 $result = mysqli_query($link, $query); $data = mysqli_fetch_all($result, MYSQLI_ASSOC); 


Native memory management


Due to the fact that mysqlnd is an extension of PHP, we have the opportunity to control (and also see the memory allocation statistics through memory_get_usage ) memory allocation when working with MySQL.

In other words, earlier, the following script could fall out of memory, only if the size of the $ data variable exceeded the memory allocation limit specified in the script, while the amount of data received from the database could be infinitely large:

 ini_set('memory_limit', '1M'); $result = mysqli_query($link, $query); $data = array(); while($row = mysqli_fetch_assoc($result)) { $data[] = $row; } 


If mysqlnd is used, such a script will fall out of memory when the amount of data from the database exceeds the memory allocation limit specified in the script.

By the way, if you use mysqlnd, if you send a request that weighs more than max_allowed_packet on the MySQL server in Linux, you will get an error indicating this: "1153: got a packet bigger than max_allowed_packet bytes", but in Windows or when using The old driver will get the error: "2006: Server has gone away."

Asynchronous requests


mysqlnd provides a way to perform asynchronous queries in MySQL, that is, there is no need to wait for the results of the query to continue the script.

To perform an asynchronous query, you must specify the MYSQLI_ASYNC flag in mysqli_query

The results of the query are checked through mysqli_poll , in case the query has worked, the results can be obtained through mysqli_reap_async_query (please note the note to this function).

Alas, until I figured out all the nuances of using asynchronous requests and I will try to present a separate review with cases in the very near future, if anyone is interested.

UPD: my post about asynchronous requests in mysqlnd can be found here

Plugins


mysqlnd provides an API for writing plugins, thanks to which very useful extensions came into being:

Mysqlnd replication and load balancing plugin

The extension allows using a simple config to automatically distribute queries in MySQL by sending all read requests to slaves, and write requests to the master.

Mysqlnd query result cache plugin

The extension allows you to cache the results of queries at the driver level in the most popular caching engines (APC, Memcache, SQLite, you can add your own)

Mysqlnd user handler plugin

The extension provides a number of hooks executed while the driver is running. Handlers for these hooks can be described in PHP, inheriting extension classes

Source: https://habr.com/ru/post/154663/


All Articles