简体   繁体   English

如何使用 PHP 7 启用 mysqli?

[英]How to enable mysqli with PHP 7?

I have the latest version of Apache (Apache 2.4), latest version of PHP (PHP7), and latest version of MySQL (not sure the version but I have WorkBench version 6.3).我有最新版本的 Apache (Apache 2.4)、最新版本的 PHP (PHP7) 和最新版本的 MySQL(不确定版本,但我有 WorkBench 版本 6.3)。 In the past, when I checked if my PHP was working by this line of code <?php phpinfo(); ?>过去,当我通过这行代码检查我的 PHP 是否正常工作时<?php phpinfo(); ?> <?php phpinfo(); ?> it would show that I have MySQL / mysqli installed and ready to go. <?php phpinfo(); ?>它会显示我已经安装了 MySQL / mysqli 并准备好了。 But with PHP7 it shows mysqlnd.但是在 PHP7 中它显示 mysqlnd。

I try using mysqli commands to check if I can connect to my database by mysqli_ping() and I also tried connecting to the database using server, user, pass.我尝试使用 mysqli 命令来检查是否可以通过mysqli_ping()连接到我的数据库,并且我还尝试使用 server、user、pass 连接到数据库。 I keep getting errors such as Class mysqli not found.我不断收到错误,例如找不到 Class mysqli。 Or Call to undefined function mysqli_ping().或者调用未定义的函数mysqli_ping()。 I've tried searching the net for some answers and All I could find was that I need to install MySQL (but a few sites said it was deprecated).我试过在网上搜索一些答案,我能找到的只是我需要安装 MySQL(但一些网站说它已被弃用)。

Am I able to get mysqli for PHP7?我可以为 PHP7 获取 mysqli 吗? Or do I use mysqlnd?还是我使用mysqlnd? What is mysqlnd?什么是mysqlnd? Is it a new version of MySQL/mysqli?是新版本的 MySQL/mysqli 吗?

Update 2020 2020 年更新

For Windows 10 users I strongly recommend windows subsystem for linux (WSL 2) and setup your server there.对于 Windows 10 用户,我强烈推荐用于 linux (WSL 2) 的windows 子系统并在那里设置您的服务器。

If you need php and mysql access in windows environment, then the fastest way is to install Wampserver .如果在windows环境下需要php和mysql访问,那么最快的方式就是安装Wampserver If you also need access to php through console then add php location (like C:\\wamp64\\bin\\php\\php7.4.11 ) to environment variables.如果您还需要通过控制台访问 php,则将 php 位置(如C:\\wamp64\\bin\\php\\php7.4.11 )添加到环境变量中。

If that doesn't suit you and you need full setup always enabled on your system then follow install with choco.如果这不适合您,并且您需要始终在系统上启用完整设置,请按照 choco 进行安装。


To work properly with apache and load all needed extensions (including php_mysqli) you need ThreadSafe version of PHP, so make sure to download correct version or if you use Chocolatey to manage your windows applications, then just install php with this command:要正常使用 apache 并加载所有需要的扩展(包括 php_mysqli),您需要ThreadSafe版本的 PHP,因此请确保下载正确的版本,或者如果您使用Chocolatey来管理您的 Windows 应用程序,那么只需使用以下命令安装 php:

choco install php --package-parameters='"/ThreadSafe "'.

This will install php in C:\\tools\\php72 directory, but you can overwrite it with another parameter:这将在C:\\tools\\php72目录中安装 php,但您可以使用另一个参数覆盖它:

choco install php --package-parameters='"/ThreadSafe ""/InstallDir:C:\PHP"""'.

The mysqlnd extension is part of PHP. mysqlnd 扩展是 PHP 的一部分。 It was developed as an alternative to bundling the libmysqlclient, because that library had license conditions that made it awkward to bundle with PHP.它是作为捆绑 libmysqlclient 的替代方案而开发的,因为该库具有许可条件,因此很难与 PHP 捆绑。

You won't use mysqlnd directly.您不会直接使用 mysqlnd。 It's an internal library that is used by mysqli and pdo_mysql as a means to communicate with the MySQL Server.它是一个内部库,被 mysqli 和 pdo_mysql 用作与 MySQL 服务器通信的一种方式。

As for mysqli, yes, it's available for PHP 7. I don't use Windows, but apparently not all extensions are enabled by default.至于 mysqli,是的,它可用于 PHP 7。我不使用 Windows,但显然并非所有扩展都默认启用。

You probably need to edit your php.ini and uncomment the line您可能需要编辑您的 php.ini 并取消注释该行

extension=ext_mysqli.dll

or whatever it is for that extension.或该扩展程序的任何内容。 It might already be in the file, but commented out.它可能已经在文件中,但被注释掉了。 So go take a look and see.所以去看看吧。

Check if your system path environmental variable includes the PHP installation directory.检查您的系统路径环境变量是否包含 PHP 安装目录。 That worked for me on with PHP 7.1 / Apache 2.4 / Windows Server 2016.这在 PHP 7.1 / Apache 2.4 / Windows Server 2016 上对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM