Skip to content

Instantly share code, notes, and snippets.

@adorilson
Forked from henriquehorbovyi/Connection.php
Last active September 1, 2015 19:53
Show Gist options
  • Select an option

  • Save adorilson/131091bc3c83c0af49e6 to your computer and use it in GitHub Desktop.

Select an option

Save adorilson/131091bc3c83c0af49e6 to your computer and use it in GitHub Desktop.
Database Connection PHP using the class PDO.
<?php
class Connection
{
public static $db;
private static function doConnection()
{
if (!isset(self::$db)) {
try {
$host = "YOUR_HOST";
$db_name = "YOUR_DB";
$user = "YOUR_USER";
$pass = "YOUR_PASS";
self::$db = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass);
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
catch (PDOException $e) {
echo "CONNECTION ERRO" . $e->getMessage();
}
}
return self::$db;
}
//ISSO AQUI NUM TEM NADA A VER
public static function prepare($sql)
{
return self::doConnection()->prepare($sql);
}
}
@henriquehorbovyi
Copy link

Thanks...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment