This is the first post of an series on the MySQL native driver or mysqlnd. In particualr I hope to cover the creation of a mysqlnd plugin both as a C extension to PHP and also as PHP code using mysqlnd_uh.
What is mysqlnd?
Quite simply mysqlnd is a PHP extension that handles talking to MySQL databases. Previously this was done via a C library from MySQL called libmysql. This library was used by many different languages to talk to a MySQL server and it worked well. However, with MySQL and PHP being so popular the MySQL team looked at improving the performance of the combination as well as being more compatible with the PHP license. In particular in the traditional setup every result set was being copied twice once within libmysql and once in the PHP mysql extensions. Mysqlnd is a PHP extension that exports no new PHP functions it has the sole job of talking to mysql servers. If installed it will be used by all the other mysql libraries such as mysqli etc as the client. It has the added benefit of not needing to be built against any one version of libmysql making it more portable.
Where can I get my mysqlnd?
You may already be using it! Mysqlnd has been in the PHP code base since PHP 5.3. It is the default mysql client on PHP windows binaries.
to find out do
to list all installed modules
[PHP Modules] apc bcmath bz2 .... mysqlnd ...
or look at the output of phpinfo(); in a browser.
If you don’t have it then I’m afraid the only way is to recompile PHP with the correct flags. These are
--with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \
Distro packagers have been reluctant to switch from libmysql as stability is of prime importance to them. If you want to build php on ubuntu to include mysqlnd see this post.
What now, I can’t see anything?
As mentioned before mysqlnd adds no new PHP functions. There is however, one PHP function that will only work if you are using this newer client. mysqli_result::fetch_all gets all the results from a results set as an array which is assoivative, numeric or both. If you try using this function with libmysql you will get an error.
<?php $mysqli = new mysqli("localhost", "nat", "supersecretpassword", "mysql"); $query = "SELECT * from db"; $result = $mysqli->query($query); $rows = $result->fetch_all(); var_dump($rows);
Fatal error: Call to undefined method mysqli_result::fetch_all() in /home/nat/test.php on line 8
The function is useful in some cases but putting large result sets into memory is generally not the best idea as it will consume large amounts of memory (not true see Ulf’s comments) and there are already functions to seek, iterate and find the size of results sets.