A database connectivity code using php

In this example, we create a PHP file to connect with database. The same file will be used and should be referred in the rest of the applications:

opendb.php

<?php

$servername = "localhost";
$username = "root";
$password = "";  // set this field "" (empty quotes) if you have not set any password in mysql
$dbname = "student"; // change this value to the name of your database

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// uncomment below code to verify connectivity with database	
//	echo "Connection successful";
}
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

?>

 

Add a Comment

Your email address will not be published. Required fields are marked *