PHP : Connect to MySQL ( mysqli and PDO)

1.  Connect to MySQL  : mysqli (OOPS)

<?php
$host = '127.0.0.1';
$user = 'root';
$pass = '';
$dbname = 'university';
$db= new  mysqli($host,$user,$pass,$dbname);

if ( ! $db->connect_error) 
{
echo 'Connection established  sucessfully...';
}
else
{
echo 'The connection to the database could not be established.';
}
?>

2. Connect to MySQL  : mysqli (Procedural)

<?php
$host = '127.0.0.1';
$user = 'root';
$pass = '';
$dbname = 'university';
$db = mysqli_connect($host, $user, $pass, $dbname);
if(mysqli_connect_errno())
{
    die('The connection to the database could not be established.');
}
else
{
	  echo " Connection established  sucessfully...";
}
?>

3.  Connect to MySQL  : PDO (PHP Data Object)

<?php
$host = '127.0.0.1';
$user = 'root';
$pass = '';
$dbname = 'university';
try
{
$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
echo " connection established  sucessfully...";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Posted in PHP