Home Programming PHP PHP MySQL Connect to Database PDO

PHP MySQL Connect to Database PDO

Connection to MySQL database using PHP with PDO.

MySQL Database and PHP: Hypertext Preprocessor
MySQL Database and PHP: Hypertext Preprocessor

Below is a sample PHP code, that after configuration, can be used to connect to your database.

<?php
$DBType = "mysql";
$DBHost = "database-host";
$DBPort = "3306";
$DBEnco = "utf-8";
$DBUser = "database-user";
$DBPass = "database-password";
$DBName = "database-name";
try {
$DBCon=new PDO("$DBType:host=$DBHost;port=$DBPort;encoding=$DBEnco;dbname=$DBName",$DBUser,$DBPass);
$DBCon->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
// Connected
} catch(PDOException $DBError) {
// Not Connected
echo '<strong>'.$DBError->getMessage().'</strong>';
exit;
}
?>