Devesh Kumar No comments

How to connect MySQL in PHP

 

Open a Connection to MySQL

Before we can access data in the MySQL database, we need to be able to connect to the server:


<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

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

Close the Connection


<?php
$conn->close();
$conn = null;
?>

Comments (0)

Post a Comment

Cancel