Created
April 4, 2017 11:04
-
-
Save VasilSlavchev/34643b31d5ec82185a690eaa62e72ae3 to your computer and use it in GitHub Desktop.
PHP OOP Connection.php
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
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