Home  >  Article  >  Backend Development  >  How to Install and Configure MySQLi for PHP on macOS?

How to Install and Configure MySQLi for PHP on macOS?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-15 09:37:02616browse

How to Install and Configure MySQLi for PHP on macOS?

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:

  • PHP 5 is installed and configured.
  • MySQL database server is up and running.

Installation Process:

  1. 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.

  2. 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:

    • macOS Mojave (10.14) and earlier: /etc/php.ini
    • macOS Catalina (10.15) and later: /usr/local/etc/php/7.4/php.ini

    Add or uncomment the following line:

    extension=mysqli.so  
  3. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn