public function connect() { if ($this->connection) { return; } if (Database_Mysql::$set_names === NULL) { // Determine if we can use mysql_set_charset(), which is only // available on PHP 5.2.3+ when compiled against MySQL 5.0+ Database_Mysql::$set_names = !function_exists('mysql_set_charset'); } extract($this->config['connection']); $host = isset($host) ? $host : $socket; $port = isset($port) ? ':' . $port : ''; try { // Connect to the database $this->connection = $this->config['persistent'] === TRUE ? mysql_pconnect($host . $port, $user, $pass, $params) : mysql_connect($host . $port, $user, $pass, TRUE, $params); } catch (Kohana_PHP_Exception $e) { // No connection exists $this->connection = NULL; // Unable to connect to the database throw new Database_Exception('#:errno: :error', array(':error' => mysql_error(), ':errno' => mysql_errno())); } if (!mysql_select_db($database, $this->connection)) { // Unable to select database throw new Database_Exception('#:errno: :error', array(':error' => mysql_error($this->connection), ':errno' => mysql_errno($this->connection))); } if (isset($this->config['character_set'])) { // Set the character set $this->set_charset($this->config['character_set']); } }
/** * 单例 * @return instance */ public static function instance() { if (self::$_instance === NULL) { self::$_instance = new self(); } return self::$_instance; }
<?php ini_set('display_errors', 'On'); error_reporting(E_ALL); if (file_exists("cache/cache.classes.php")) { $dict = unserialize(file_get_contents("cache/cache.classes.php")); } else { $dict = array(); } include "model.database.php"; $db = new Database_Mysql(); include "config.php"; $db->connect(); include "controller.action.php"; $q = $db->query("SELECT * FROM rels;"); $APP = array(); $APP['db'] = $db; // 2-line front controller $BASE_PATH = str_replace("index.php", "", $_SERVER['SCRIPT_NAME']); $RESOURCE_REQUEST = substr($_SERVER['REQUEST_URI'], strlen($BASE_PATH), strlen($_SERVER['REQUEST_URI']) - strlen($BASE_PATH)); if ($RESOURCE_REQUEST == "?") { $RESOURCE_REQUEST = ""; } if (strstr($RESOURCE_REQUEST, "?") !== false) { $RESOURCE_REQUEST = explode("?", $RESOURCE_REQUEST); $RESOURCE_REQUEST = $RESOURCE_REQUEST[0]; } $reserved = array(); include "reserved_words.php.php"; include "reserved_words.net.php"; include "functions.php";
function testConnect(&$error) { try { if (Config::get('db') == 'mysql') { require_once MTTPATH . 'class.db.mysql.php'; $db = new Database_Mysql(); $db->connect(Config::get('mysql.host'), Config::get('mysql.user'), Config::get('mysql.password'), Config::get('mysql.db')); } else { if (false === ($f = @fopen(MTTPATH . 'db/todolist.db', 'a+'))) { throw new Exception("database file is not readable/writable"); } else { fclose($f); } if (!is_writable(MTTPATH . 'db/')) { throw new Exception("database directory ('db') is not writable"); } require_once MTTPATH . 'class.db.sqlite3.php'; $db = new Database_Sqlite3(); $db->connect(MTTPATH . 'db/todolist.db'); } } catch (Exception $e) { $error = $e->getMessage(); return 0; } return 1; }
public function _get_tables($sql, $type = Database::SELECT) { $this->_type = $type; return parent::_get_tables($sql); }
function testConnect(&$error) { global $config; try { if ($config['db'] == 'mysql') { require_once 'class.db.mysql.php'; $db = new Database_Mysql(); $db->connect($config['mysql.host'], $config['mysql.user'], $config['mysql.password'], $config['mysql.db']); } else { if (false === ($f = @fopen('./db/todolist.db', 'a+'))) { throw new Exception("database file is not readable/writable"); } else { fclose($f); } } } catch (Exception $e) { $error = $e->getMessage(); return 0; } return 1; }
<?php require_once 'common.php'; require_once './db/config.php'; ini_set('display_errors', 'On'); # MySQL Database Connection if ($config['db'] == 'mysql') { require_once 'class.db.mysql.php'; $db = new Database_Mysql(); $db->connect($config['mysql.host'], $config['mysql.user'], $config['mysql.password'], $config['mysql.db']); $db->dq("SET NAMES utf8"); } else { require_once 'class.db.sqlite3.php'; $db = new Database_Sqlite3(); $db->connect('./db/todolist.db'); } $needAuth = isset($config['password']) && $config['password'] != '' ? 1 : 0; if ($needAuth) { if (isset($config['session']) && $config['session'] == 'files') { session_save_path(realpath('./tmp/sessions/')); ini_set('session.gc_maxlifetime', '1209600'); # 14 days session file minimum lifetime ini_set('session.gc_probability', 1); ini_set('session.gc_divisor', 10); } ini_set('session.use_cookies', true); ini_set('session.use_only_cookies', true); session_set_cookie_params(1209600, substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/') + 1)); #14 days session cookie lifetime session_start(); }