Maison >développement back-end >tutoriel php >Comment installer et configurer MySQLi pour PHP sur macOS ?

Comment installer et configurer MySQLi pour PHP sur macOS ?

Patricia Arquette
Patricia Arquetteoriginal
2024-11-15 09:37:02748parcourir

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.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn