W3docs

How to install MySQLi on MacOS

To install MySQLi on a Mac, you will first need to install the MySQL database server on your machine.

MySQLi is a PHP extension used to connect to MySQL or MariaDB databases. It does not require a local MySQL server installation; you can connect to any remote or local database instance. To use MySQLi, you first need PHP installed on your machine. If you don't have PHP installed, you can install it using Homebrew (recommended for modern macOS):

  1. Open a terminal and run the command brew update. This will update the list of available packages.
  2. Run the command brew install php. This will install PHP on your machine.

Once PHP is installed, verify if the mysqli extension is enabled. Create a file called phpinfo.php in your web server's root directory with the following contents:

Example of using the phpinfo() function to check if the mysqli extension is enabled

<?php
phpinfo();

Then, open a web browser and navigate to http://localhost/phpinfo.php. This will display a page with information about your PHP installation, including a list of enabled extensions. Look for the mysqli section in the list. If it is present and enabled, MySQLi is ready to use. If it is missing, enable it by adding the following line to your php.ini file:

Enable the mysqli extension in php.ini

extension=mysqli

(Note: The exact path to php.ini and the directive may vary slightly depending on your PHP version and macOS setup.)

Then, restart your web server to apply the changes. You should now be able to use MySQLi to connect to MySQL from PHP.

Example of connecting to a MySQL database using MySQLi

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>