有没有办法只用 php.ini 来改变 mysqli 使用的库?

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

所以我尝试运行共享主机上的以下代码。

<?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();

在那里它产生以下输出:

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

它在我的本地机器上产生以下预期输出:

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

据我了解,问题是 mysqli 没有使用 mysqlnd。我用 phpinfo() 进行了测试,我拍了一张我认为是相关部分的照片:

配置命令:

'./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'

在那里你可以清楚地看到 mysqli 使用 5.5.47-MariaDB 而不是 mysqlnd 显然已安装。我本地机器上的相同部分 here.

我可以选择不同的 PHP 版本,但由于其中 none 的工作方式与我预期的一样,我选择了 5.6.17,这是我可以选择的最新版本。

那么有没有办法只用 php.ini 来改变这个呢?即使有可能,这是个好主意吗?

据我所知,您必须在 编译 时决定 mysqli 使用的客户端库,并且不能在运行时更改。详情见mysqli's documentation on choosing a client library

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.

虽然上面的引用没有明确提到 mariadb,但是,可以安全地假设 mariadb 也是如此。由于您的实时代码位于共享托管环境中,因此只有提供商可以为您重新编译 php。但是,我认为他们不会只为 1 个客户(您)这样做。我会检查我是否可以更改我的代码并使其在没有 get_result() 方法的情况下工作。