To create a connection between PHP and MySQL on working locally (local server) make sure you have started your Apache Server and MySQL server. After that make a file name connect.php where your connection code will be written.
File name -> connection.php
$servername = "localhost";
$username = "username";
$password = "password";
$db = "dbname";
$con=mysqli_connect($servername,$username,$password,$db) or die("error");
To connect PHP and MySQL Database we use the function mysql_connect. This function returns database handle.
Create a new PHP file and name it connnection.php and save it. I am creating a separate database connection file because if we have created multiple files in which we want to insert data or select data from the databases, we don’t need to write the code for database connection every time we can include it by using PHP custom function include (include ‘connection.php’) on the top of your code and call its function and use it.