Setting Up MySQLi for PHP on macOS
For those facing difficulties installing MySQLi on their Mac environment, this guide provides step-by-step instructions to simplify the process.
Prerequisites:
Installation Process:
Install php-mysqlnd:
Php-mysqlnd is a drop-in replacement for php-mysql, offering better performance and stability. To install it, run the following command:
brew install php@7.4 --with-mysqlnd
Replace "php@7.4" with the version of PHP you have installed.
Enable MySQLi Extension:
After installing php-mysqlnd, you need to enable the MySQLi extension in your PHP configuration. Edit the following file based on your OS version:
Add or uncomment the following line:
extension=mysqli.so
Restart Apache:
After making the changes to the php.ini file, restart Apache to apply them:
sudo apachectl restart
Testing MySQLi:
To verify that MySQLi is installed and configured correctly, create a simple PHP script:
<?php $mysqli = new mysqli("localhost", "username", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } else { echo "Connection to MySQL server succeeded."; } ?>
Run the script to check if it connects to the MySQL database without any errors.
The above is the detailed content of How to Install and Configure MySQLi for PHP on macOS?. For more information, please follow other related articles on the PHP Chinese website!