protected function conectar() { if (!self::$conn) { if (DEBUG_MODE) { new DebugMsg('Abriu uma nova conexão com MySQL'); } $this->host = Config::get('db_host'); $this->user = Config::get('db_user'); $this->pass = Config::get('db_password'); $this->dbname = Config::get('db_name'); $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; // Set options $options = array(PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); try { $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); self::$conn = $this->dbh; } catch (PDOException $e) { $this->error = $e->getMessage(); $log = fopen('logs/log_db_error.txt', 'a+'); fwrite($log, date("d/M/Y H:i") . ' - ' . $this->error . "\r\n"); fwrite($log, __FILE__ . "\r\n"); fwrite($log, "================================================\r\n\n"); fclose($log); if (DEBUG_MODE) { echo '<h1>' . __('Verifique o arquivo config.php') . '</h1>'; echo '<p>' . __('nao foi possivel conectar no banco de dados') . '</p>'; } exit; } } $this->dbh = self::$conn; }
$config['password'] = isset($arr['password']) ? $arr['password'] : ''; $config['dbName'] = isset($arr['dbName']) ? $arr['dbName'] : ''; $config['port'] = isset($arr['port']) ? $arr['port'] : 3306; $config['charset'] = isset($arr['charset']) ? $arr['charset'] : 'utf8'; self::$configs[$config['connectionName']] = $config; if (isset(self::$dbConnectionPool[$config['connectionName']])) { unset(self::$dbConnectionPool[$config['connectionName']]); } return true; } public static function getConnection($connectionName) { if (!is_string($connectionName) || !isset(self::$configs[$connectionName])) { throw new Exception("mysql config : {$connectionName} is't loaded !", -1); } if (!isset(self::$dbConnectionPool[$connectionName])) { $config = self::$configs[$connectionName]; $dbConnection = new MysqlOption($connectionName, $config['host'], $config['userName'], $config['password'], $config['dbName'], $config['port'], $config['charset']); self::$dbConnectionPool[$connectionName] = $dbConnection; } return self::$dbConnectionPool[$connectionName]; } } //测试 if (strtolower(PHP_SAPI) == 'cli' && basename(__FILE__) == basename($argv[0])) { $config = array('connectionName' => 'dbLol', 'host' => '*.*.*', 'userName' => 'xxxx', 'password' => '*****', 'dbName' => 'test', 'port' => '3306'); MysqlDB::loadOneConfig($config); $lolConnection = MysqlDB::getConnection('dbLol'); $data = $lolConnection->query('select * from test where id<=?;', array(10)); print_r($data); }
private function getTableDescription($tablename) { $db = new MysqlDB(); $db->query("DESCRIBE {$tablename}"); $r = $db->getResults(); $desc = array(); foreach ($r as $rvalue) { $desc[] = $rvalue->Field; } return $desc; }
<?php include 'mysql-db.php'; $DB = new MysqlDB('localhost', 'root', 'mysql', 'oop'); $DB->where('id', 2); $rows = $DB->get('mysql'); /* * query and retrive data form database with OOP * To change this template file, choose Tools | Templates * Developed by Anam */ ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>MysqlDB</title> </head> <body> <?php foreach ($rows as $row) { ?> <?php // var_dump($results); ?> <h2><?php echo $row['title']; ?> </h2>
public static function setDebug($on = true) { self::$debug = (bool) $on; }
protected function cancelTransaction() { $db = new MysqlDB(); $db->cancelTransaction(); }
/** Authors: Herman Tolentino MD (herman.tolentino@gmail.com) Date: 2006 April 1 License: GNU GPL (open source) EpiSPIDER Project IMPORTANT: Implementation of this code requires that you ask permission from and make reference to the work of ProMEDMail.org USE: 1. crontab epispider.cron (runs hourly downloads) 2. see MySQL tables for data stream **/ require "class.mysql.php"; require "class.promed.php"; $mysqldb = new MysqlDB(); //$conn = $mysqldb->connect("epispider", "epispider", "db.berlios.de", "epispider"); $mysqldb->connect("root", "", "localhost", "epispider"); //$mysqldb->connect("hi4devo_epispide", "epispider", "localhost", "hi4devo_epispider"); $promed = new Promed(); $promed->dbconnect($mysqldb); $fp = $promed->connect("http://www.promedmail.org"); $promed->extract_links($fp); $promed->disconnect($fp); $promed->warehouse();