Skip to content

Instantly share code, notes, and snippets.

@bienvenuelisis
Created December 14, 2021 16:11
Show Gist options
  • Save bienvenuelisis/5aba08671a7cb90d7a76967e54dcea57 to your computer and use it in GitHub Desktop.
Save bienvenuelisis/5aba08671a7cb90d7a76967e54dcea57 to your computer and use it in GitHub Desktop.
Slim PHP Framework : DB Connection Sample
<?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();
}
}
}
<?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