-
-
Save themasch/571754 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
class db { | |
// Eigenschaft um die Datenbankverbindung zu speichern. | |
protected static $con = null; | |
// Zugangsdaten als Parameter weil global böse ist | |
public static function createConnection($host, $name, $user, $pass) { | |
// neue Verbindung erzeugen und in Klasse speichern. | |
self::$con = new mysqli($host, $user, $pass, $name); | |
/* check connection */ | |
if (mysqli_connect_errno()) { | |
printf("Connect failed: %s\n", mysqli_connect_error()); | |
exit(); | |
} | |
} | |
// Methode um auf die Verbindung zuzugreifen | |
public static function getConnection() { | |
// gibt nur die Verbindung zurück | |
return self::$con; | |
} | |
} | |
/** | |
* usage: | |
* DB::createConnection('host', 'name', 'user', 'pass'); | |
* ...viel...viel..code.. | |
* $mysql = DB::getConnection(); | |
* $mysql->query('FOOBAR'); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment