public static function getInstance() { if (empty(MyDb::$instance)) { MyDb::$instance = new MyDb(); } return MyDb::$instance; }
/** * Setup Test */ protected function setUp() { $hostname = $GLOBALS["hostname"]; $username = $GLOBALS["username"]; $password = $GLOBALS["password"]; $dbname = $GLOBALS["dbname"]; $this->db = MyDb::getInstance('MySQLDb', $hostname, $username, $password, $dbname, 3306); }
public static function dumpData() { require_once 'MyDb.php'; $dbh = new MyDb(); $sql = 'SELECT * FROM short_urls;'; $result = $dbh->query($sql); var_dump($result->fetchAll()); }
<?php require_once __DIR__ . '/include.php'; $db = MyDb::getInstance(); /** * Make sure all individual pings are running for all hosts */ $res = $db->query('SELECT * FROM hosts'); while ($hostData = $res->fetch_array()) { $pidFile = $_config['pidFilePath'] . '/' . $hostData['id']; // Check if we have fresh data for host $dataCheck = $db->fetchOne("SELECT time FROM `ping-result` WHERE host_id = {$hostData['id']} ORDER BY id DESC LIMIT 1"); if (time() - strtotime($dataCheck['time']) < 5) { // Fresh data, no need to do anything. Next host please! continue; } // Need to kill existing process, as it might have hung, or just cleanup PID file if (file_exists($pidFile) && file_exists("/proc/" . file_get_contents($pidFile))) { $pid = file_get_contents($pidFile); `kill -9 {$pid}`; } if (file_exists($pidFile)) { unlink($pidFile); } // Starting new thread for ping for this host $runFile = "nohup php " . __DIR__ . "/ping-host.php {$hostData['id']} > /dev/null 2> /dev/null </dev/null & "; `{$runFile}`; } /** * Consolidate every minute for ease of charting, and cleanup old data */
$query = sprintf("SELECT 1 AS %s", $dbODBC->escape($provider)); // Associate print_r($dbODBC->executeCommand($query)); // Enumerate print_r($dbODBC->executeCommand($query, null, MyDb::ENUM)); $dbODBC = null; // PostgreSQL Example $provider = 'PostgreSQLDb'; $hostname = ''; $username = ''; $password = ''; $dbname = ''; $dbPgSQL = MyDb::getInstance($provider, $hostname, $username, $password, $dbname, 5432); $query = sprintf("SELECT 1 AS %s", $dbPgSQL->escape($provider)); // Associate print_r($dbPgSQL->executeCommand($query)); // Enumerate print_r($dbPgSQL->executeCommand($query, null, MyDb::ENUM)); $dbPgSQL = null; // Mysql Singleton Example $provider = 'MySQLDb'; $hostname = ''; $username = ''; $password = ''; $dbname = ''; $db = MyDb::getConnection($provider, $hostname, $username, $password, $dbname, 3306); $query = sprintf("SELECT 1 AS %s", $dbMySQL->escape($provider)); // Associate print_r($dbMySQL->executeCommand($query)); // Clone in not permitted $dbclone = clone $db;
function SaveHat($sql, $suppliers) { try { $conn = new MyDb(); $id = $conn->SaveDataWithTransaction($sql, true, false); if ($id != 0) { $suppliers_sql = " insert into map_hat_supplier "; $supplier_array = explode(',', $suppliers); foreach ($supplier_array as $sid) { $dataArray = array("map_hat_id" => "'" . $id . "'", "map_supplier_id" => "'" . $sid . "'"); $sqltemp = $suppliers_sql . parent::GetInsertSQL($dataArray); $conn->SaveDataWithTransaction($sqltemp, false, false); } } $conn->CommitTransaction(); $conn->Close(); return $id; } catch (Exception $e) { $conn->Close(); echo $e->getMessage(); } return 0; }
/** * Implements Singleton Pattern * * @param string $provider A valid provider if Abstract Class * @param string $hostname A valid hostname * @param string $username A valid user in RDBMS * @param string $password A valid password in RDBMS * @param string $dbname A valid database in RDBMS (For ODBC is a Data Source Name (DSN)) * @param int $port RDBMS Listen Port * @return resource | null */ public static function getConnection($provider, $hostname, $username, $password, $dbname, $port) { // If exists Instance return same Instance if (self::$_instance) { return self::$_instance; } else { $class = __CLASS__; self::$_instance = new $class($provider, $hostname, $username, $password, $dbname, $port); return self::$_instance; } }