PHP Code To Connect To MySQLĬreate a new PHP file named Connect_data.php under the /htdocs directory with the following contents in it. Now, let us write a sample PHP code based on the above steps. This function will display an error, if the connection is failed. We can check the connection using the mysqli_connect_error() function specified in an if condition. It will also take a database name which is optional here, because we are only creating the connection.Ĭode: $connection = mysqli_connect($server_name, $user_name, $password)
First will be the servername, second is the username (i.e. Create a connection using the above details.īy using mysqli_connect() function, we will establish a connection. Here, my servername is localhost, MySQL username is root and its password is empty.Ģ. Specify MySQL servername, username and password parameters in your PHP code. So, we will will be using XAMPP stack throughout this guide. Setting up XAMPP is much easier than LAMP and LEMP stacks. Install Nginx, MariaDB, PHP (LEMP) stack on Arch Linux.Install Apache, MariaDB, PHP (LAMP) stack on Arch Linux.Install Apache, MariaDB, PHP (LAMP Stack) In CentOS 8.
Install Nginx, MySQL, PHP (LEMP Stack) On Ubuntu 20.04 LTS.Install Apache, MySQL, PHP (LAMP Stack) On Ubuntu 20.04 LTS.If you're on Linux, refer the following guides to install LAMP/LEMP stacks.
Alternatively, you can use the LAMP or LEMP stacks which provides both PHP and MySQL.