PHP MySql Connect & Close

PHP have 3 ways to connect with Mysql and other Databases. below examples will demonstrate the MySqli (Object Oriented) and Mysqli Procedural Methods.

MySQL Connect & Close (Object Oriented)


<?php

$server="localhost";
$username="root";
$password="";
$db="e-commerce";

$conn=new mysqli($server,$username,$password,$db);

if($conn->connect_error){
	die("Error:".$conn->connect_error);
}

echo 'Successfully Connected:)';

$conn-close();
?>

MySQL Connect & Close (Procedural)


<?php

$server="localhost";
$username="root";
$password="";
$db="e-commerce";

$conn=mysqli_connect($server,$username,$password,$db);

if(!$conn){
	die("Error:".mysqli_connect_error());
}

echo 'Successfully Connected:)';

mysqli_close($conn);
?>