Skip to content

Instantly share code, notes, and snippets.

@themasch
Forked from anonymous/mysqli_soomon
Created September 9, 2010 11:31
Show Gist options
  • Save themasch/571754 to your computer and use it in GitHub Desktop.
Save themasch/571754 to your computer and use it in GitHub Desktop.
<?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