Skip to content

Instantly share code, notes, and snippets.

@VasilSlavchev
Created April 4, 2017 11:04
Show Gist options
  • Save VasilSlavchev/34643b31d5ec82185a690eaa62e72ae3 to your computer and use it in GitHub Desktop.
Save VasilSlavchev/34643b31d5ec82185a690eaa62e72ae3 to your computer and use it in GitHub Desktop.
PHP OOP Connection.php
class Database {
public $conn;
protected $hostname;
protected $username;
protected $password;
protected $dbname;
public function __construct ( $hostname, $username, $password, $dbname ) {
global $conn;
$this->conn = $conn;
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
$conn = new mysqli($hostname, $username, $password, $dbname) or die(('Could not connect to this hostname.' .$hostname. 'Error: ' . mysqli_connect_error() . $conn->connect_error);
if ($conn == false) {
trigger_error('Error connecting: ' . mysqli_connect_error() . $conn->error . $conn->errno, E_USER_ERROR);
exit;
}
$conn->set_charset('utf8');
}
}
// creating the object of connection.
$db = new Database ( 'localhost', 'root', '', 'table_name' );
// debug
print '<pre>';
print_r($db);
print '</pre>';
print '<pre>';
var_dump($db);
print '</pre>';
print '<hr>';
print '<pre>';
print_r($conn);
print '</pre>';
print '<pre>';
var_dump($conn);
print '</pre>';
print '<hr>';
printf("<pre>%s</pre>",print_r($conn, true));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment