/** * Static method get * * @param array $group * @return \helpers\database */ public static function get($group = false) { // Determining if exists or it's not empty, then use default group defined in config $group = !$group ? array('type' => DB_TYPE, 'host' => DB_HOST, 'name' => DB_NAME, 'user' => DB_USER, 'pass' => DB_PASS) : $group; // Group information $type = $group['type']; $host = $group['host']; $name = $group['name']; $user = $group['user']; $pass = $group['pass']; // ID for database based on the group information $id = "{$type}.{$host}.{$name}.{$user}.{$pass}"; // Checking if the same if (isset(self::$instances[$id])) { return self::$instances[$id]; } $instance = new Database("{$type}:host={$host};dbname={$name};charset=utf8", $user, $pass); $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Setting Database into $instances to avoid duplication self::$instances[$id] = $instance; return $instance; }
private function setupEnvironment() { global $gDatabase, $cDatabaseConnectionString, $cMyDotCnfFile, $cDatabaseModule, $cIncludePath, $cLoggerName, $gLogger; set_exception_handler(array("Hotel", "exceptionHandler")); // check all the required PHP extensions are enabled on this SAPI $this->checkPhpExtensions(); // start output buffering before anything is sent to the browser. ob_start(); // not caught by the autoloader :( require_once 'smarty/Smarty.class.php'; // many exceptions defined in one file, let's not clutter stuff. // This ofc breaks the autoloading, so let's include them all now. // (Depends on some smarty stuff) require_once $cIncludePath . "/_Exceptions.php"; spl_autoload_register("Hotel::autoLoader"); Session::start(); $gLogger = new $cLoggerName(); $gLogger->log("Initialising logger!"); if (!extension_loaded($cDatabaseModule)) { throw new ExtensionUnavailableException($cDatabaseModule); } $mycnf = parse_ini_file($cMyDotCnfFile); $gDatabase = new Database($cDatabaseConnectionString, $mycnf["user"], $mycnf["password"]); // tidy up sensitive data we don't want lying around. unset($mycnf); // use exceptions on failed database stuff $gDatabase->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // can we tidy up the output with tidy before we send it? if (extension_loaded("tidy")) { // Yes! global $cUseTidy; if ($cUseTidy) { // register a new function to hook into the output bit Hooks::register("BeforeOutputSend", function ($params) { $tidy = new Tidy(); global $cTidyOptions; return $tidy->repairString($params[0], $cTidyOptions, "utf8"); }); } } global $gCookieJar; $gCookieJar = array(); }
public function delete() { try { // Open database connection $database = new Database(); // Set the error reporting attribute $database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Build database statement $sql = "delete from stats where id = :id limit 1"; $statement = $database->prepare($sql); $statement->bindParam(':id', $this->id, PDO::PARAM_INT); // Execute database statement $statement->execute(); // Get affected rows $count = $statement->rowCount(); // Close database resources $database = null; // Return affected rows return $count; } catch (PDOException $exception) { die($exception->getMessage()); } }
$csvFile = TMP_DIR . 'GeoIP/GeoIPCountryWhois.csv'; if (file_exists($csvFile)) { unlink($csvFile); } echo " Extracting..."; $archive = new \ZipArchive(); if ($archive->open($zipFile) !== true) { throw new \Exception('Failed to open zipfile'); } $archive->extractTo(TMP_DIR . 'GeoIP/'); echo " done\n"; // Rebuild echo " Creating database..."; $db = new Database('sqlite:' . $dbFile); Database::$instances['GeoIP Import'] = $db; $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); // end script on a sql error $db->query('CREATE TABLE country ( code CHAR(2) PRIMARY KEY, name VARCHAR(150) NOT NULL )'); if ($VERBOSE) { $db->query('CREATE TABLE ip2country ( begin UNSIGNED INTEGER PRIMARY KEY, end UNSIGNED INTEGER NOT NULL, ip_begin TEXT NOT NULL, ip_end TEXT NOT NULL, country_code CHAR(2) NOT NULL REFERENCES country(code) )'); } else { $db->query('CREATE TABLE ip2country (