【问题标题】:Is there a way to change what library mysqli uses with just the php.ini?有没有办法只用 php.ini 来改变 mysqli 使用的库?
【发布时间】:2016-04-25 05:08:55
【问题描述】:

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

<?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
致命错误:在第 29 行的 /home/user/public_html/beta/test_mysqli_asmallorange.php 中调用未定义的方法 mysqli_stmt::get_result()

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

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

据我了解,问题在于 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 版本,但由于它们都不像我预期的那样工作,所以我选择了5.6.17,这是我可以选择的最新版本。

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

【问题讨论】:

  • 也许你的php版本低于5.3?
  • PHP Version 5.6.17我会在问题中更清楚地说明这一点。
  • phpinfo() 输出之上的配置命令是什么?

标签: php mysql mysqli mysqlnd


【解决方案1】:

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

mysqli、PDO_MySQL 和 mysql PHP 扩展是轻量级的 C 客户端库之上的包装器。扩展可以使用 mysqlnd 库或 libmysqlclient 库。选择图书馆 是一个编译时间决定。

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

【讨论】:

  • 感谢您的回答。不是我希望的答案。 :( 有没有理由甚至包含 mysqlnd ?
  • mysqli 不是唯一可用于访问 mysql 的 API。旧的 mysql 和 PDO API 也可以使用。其中任何一个都可以配置为使用 mysqlnd。有关详细信息,请参阅php.net/manual/en/mysqli.installation.php(第一个表正下方的段落)。
  • 您的托管服务似乎将 mysqlnd 用于 PDO。
猜你喜欢
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 2019-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多