Training Institute in Noida | Delhi NCR No.❶ Digital Marketing Training Institute in Noida | Delhi NCR
img

Connecting PHP to MySQL Database -PHP Training Institute

Connecting PHP to MySQL Database
PHP

Connecting PHP to MySQL Database

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

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

Let's Update Your Skills with Our Training Professionals...

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.