Search code examples
phpmysqlmysqlimysqlnd

Is there a way to change what library mysqli uses with just the php.ini?


So I try to run the following code on a shared hosting.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
echo phpversion();
$mySqlServername = "localhost";
$mySqlUsername = "auser";
$mySqlPassword = "apassword";
$mySqlDbname = "adatabase";
//Create Connection
$conn = new mysqli($mySqlServername, $mySqlUsername, $mySqlPassword, $mySqlDbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Set charset to UTF-8
if (!$conn->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $conn->error);
exit();
}
if ($currConn = $conn->prepare("SELECT DISTINCT subject FROM topics")) {
    $currConn->execute();
    $result = $currConn->get_result();
    $currConn->close();
    /*for ($i = 0; $i < $result->num_rows; $i++) {
        $array[$i] = $result->fetch_row()[0];
    }*/
    var_dump($result->fetch_row());
} else {
    die("Statement could not be prepared!");
}
$conn->close();

On there it produces the following output:

5.6.17
Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/user/public_html/beta/test_mysqli_asmallorange.php on line 29

Where on my local machine it produces the following expectet Output:

5.6.11-1ubuntu3.1array(1) { [0]=> string(8) "Religion" }

And as far as I understand the problem is that mysqli is not using mysqlnd. Wich I tested with phpinfo() and I took a picture of what I think is the relevent section:

enter image description here

Configure Command:

'./configure' '--exec-prefix=/usr/local/php56' '--prefix=/usr/local/php56' '--with-config-file-scan-dir=/usr/local/php56/conf.d' '--with-libdir=lib64' '--enable-bcmath' '--enable-calendar' '--enable-exif' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-mbstring' '--enable-pdo' '--enable-soap' '--enable-sockets' '--enable-wddx' '--enable-zip' '--with-apxs2=/usr/local/apache/bin/apxs' '--with-bz2' '--with-curl=/opt/curlssl/' '--with-freetype-dir=/usr' '--with-gd' '--with-gettext' '--with-imap-ssl=/usr' '--with-imap=/opt/php_with_imap_client/' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libexpat-dir=/usr' '--with-mcrypt=/opt/libmcrypt/' '--with-mhash=/opt/mhash/' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-mysql=/usr' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl-dir=/usr' '--with-openssl=/usr' '--with-pcre-regex' '--with-pdo-mysql=shared' '--with-pdo-sqlite=shared,/usr' '--with-pdo-pgsql=shared' '--with-pgsql=shared,/usr/lib64/pgsql' '--with-pic' '--with-png-dir=/usr' '--with-pspell' '--with-snmp' '--with-tidy' '--with-libxml-dir=/opt/xml2/' '--with-xmlrpc' '--with-xpm-dir=/usr' '--with-xsl=/opt/xslt/' '--with-zlib' '--with-zlib-dir=/usr' '--enable-mysqlnd' '--enable-intl'

There you can clearly see that mysqli uses 5.5.47-MariaDB and not mysqlnd wich is clearly installed. Same section on my local machine here.

I can choose diffrent PHP versions, but since none of them work like I expect I settled for 5.6.17 which is the most recent version I can choose.

So is there a way to change this with just the php.ini or not? And even if it is possible is it a good idea?


Solution

  • As far as I know you have to make the decision what client library mysqli uses at compile time, and cannot be changed during runtime. See mysqli's documentation on choosing a client library for details:

    The mysqli, PDO_MySQL and mysql PHP extensions are lightweight wrappers on top of a C client library. The extensions can either use the mysqlnd library or the libmysqlclient library. Choosing a library is a compile time decision.

    Although the above quote does not explicitly mention mariadb, however, it is safe to assume that this is the case for mariadb as well. Since your live code is in a shared hosting environment, only the provider can recompile php for you. However, I do not think that they will do that just for 1 client (you). I would check if I can change my code and make it work without the get_result() method.