Created
December 14, 2021 16:11
-
-
Save bienvenuelisis/5aba08671a7cb90d7a76967e54dcea57 to your computer and use it in GitHub Desktop.
Slim PHP Framework : DB Connection Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Database { | |
class Db | |
{ | |
private $servername = "localhost"; | |
private $username = "root"; | |
private $password = ""; | |
private $database = ""; | |
public $conn = ""; | |
public function _construct() | |
{ | |
try { | |
$this->conn = new \PDO("mysql:host=$this->servername;dbname=$this->database", $this->username, $this->password, | |
array(\PDO::MYSQL_ATTR_INIT_COMMAND)); | |
// set the \PDO error mode to exception | |
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); | |
$this->conn->exec("set names utf8"); | |
// echo "Connected successfully"; | |
} catch (\PDOException $e) { | |
echo "Connection failed: " . $e->getMessage(); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include "Db.php"; | |
$db = new \Database\Db(); | |
$db->_construct(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment