/** * Constructs a new SQL storage object. * * @param array $params A hash containing connection parameters. */ function Klutz_Driver_sql($params = array()) { if (empty($params['basedir'])) { return null; } $this->basedir = $params['basedir']; if (substr($this->basedir, -1, 1) != "/") { $this->basedir .= "/"; } /* Setup the database */ $config = $GLOBALS['conf']['sql']; unset($config['charset']); $this->_db = MDB2::factory($config); $this->_db->setOption('seqcol_name', 'id'); }
function db_init($user, $passwd, $host, $database, $die_on_error = TRUE, $charset = "UTF8") { global $db; // DSN string must have new_link=true in order for multiple connections with different charsets to work $dsn = "mysql://{$user}:{$passwd}@{$host}/{$database}?new_link=true"; $db = MDB2::factory($dsn); if (PEAR::isError($db)) { $msg = "Unable to initialise database. Please try again later."; $msg = "{$msg}: {$db->message} . ' ' . {$db->userinfo}"; $db = NULL; db_log(1, $msg); if ($die_on_error) { if (defined('UNIT_TEST')) { print $msg . "\n"; } exit; } } else { $db->setCharset($charset); // remove MDB2_PORTABILITY_EMPTY_TO_NULL - Without turning this setting off, MDB2 will treat empty strings (i.e. '') // as NULLs when storing. We prefer to use empty strings for some fields. // remove MDB2_PORTABILITY_FIX_CASE - keep associative fields case sensitive rather than all lower case $db->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL ^ MDB2_PORTABILITY_FIX_CASE); } }
/** * connect() * * @access public * @param string $sDsn * @param mixed $mOptions boolean | array * @return mixed boolean | MDB2_Error */ function connect($sDsn, $mOptions = array()) { if (isset($this->_aryConn[$sDsn])) { $mRet = $this->setOptions($sDsn, $mOptions); if (MDB2::isError($mRet)) { return $mRet; } $this->_aryConn[$sDsn]->debug_output = ''; return true; } if (!is_array($mOptions)) { $mOptions = array('persistent' => $mOptions); } $this->_aryConn[$sDsn] = MDB2::factory($sDsn, $mOptions); if (MDB2::isError($this->_aryConn[$sDsn])) { $objConn = $this->_aryConn[$sDsn]; unset($this->_aryConn[$sDsn]); return $objConn; } $this->_aryConn[$sDsn]->loadModule('Date'); $this->_aryConn[$sDsn]->loadModule('Extended'); $this->_aryConn[$sDsn]->setFetchMode(MDB2_FETCHMODE_ASSOC); $this->_aryConn[$sDsn]->setCharset('UTF8'); $this->_aryConn[$sDsn]->debug_output = ''; return true; }
public function createInstance() { require_once 'MDB2.php'; $dsn = $this->settings['db.dsn']; $db = MDB2::factory($dsn); return $db; }
function conectar() { if (!isset($mdb2)) { //Local $db_name = 'root'; $db_password = ''; $db_server = 'localhost'; $db_database = 'coopcrucial'; //Nabica /*$db_name = 'coopcrucial'; $db_password = '******'; $db_server = 'coopcrucial.db.5840507.hostedresource.com'; $db_database = 'coopcrucial';*/ $dsn = 'mysql://' . $db_name . ':' . $db_password . '@' . $db_server . '/' . $db_database; try { $mdb2 =& MDB2::factory($dsn, true); if (MDB2::isError($mdb2)) { die($mdb2->getmessage() . ' - ' . $mdb2->getUserInfo()); } else { $mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC); } $mdb2 = array('mdb2' => $mdb2, 'dsn' => $dsn); } catch (Exception $exc) { echo $exc->getTraceAsString(); } } return $mdb2; }
public function dbh($dsn = null, $opt = null) { //init if (null != $dsn) { $this->dsn = $dsn; } if (null != $opt) { $this->opt = $opt; } //connect => $dsn = 'pgsql://*****:*****@localhost/thedb'; $this->dbh =& MDB2::factory($this->dsn, $this->opt); //sanity if (self::err($this->dbh, "dbh() : init-db")) { return null; } //re-init $this->dbh->setFetchMode(MDB2_FETCHMODE_ASSOC); //more options $this->dbh->setOption('result_buffering', true); $this->dbh->setOption('multi_query', true); //set here the utf-8 $utf8[] = " SET NAMES 'utf8' COLLATE utf8_unicode_ci "; $utf8[] = " SET character_set_client='utf8' "; $utf8[] = " SET character_set_connection='utf8' "; for ($i = 0; $i < @count($utf8); $i++) { $bfsql = trim($utf8[$i]); self::query($bfsql, "ERROR: {$bfsql}"); } //give it back ;-) return $this->dbh; }
private function __construct() { Plank_Logger::log('Singleton', get_class($this) . ' singleton created.', L_TRACE); $config = Plank_Config::getInstance(); $config = $config->getArea('database'); if (!$config || !isset($config['connections'])) { throw new Plank_Exception_Database_Config('Databases not configured'); } $connections = explode(',', $config['connections']); foreach ($connections as $connection) { if (isset($config['dsn_' . $connection])) { $this->connections[$connection] = MDB2::factory($config['dsn_' . $connection]); if (PEAR::isError($this->connections[$connection])) { throw new Plank_Exception_Database('DB Error! ' . $this->connections[$connection]->getMessage() . ' ' . $this->connections[$connection]->getUserInfo()); } if (SHOWDEBUG) { $this->connections[$connection]->setOption('debug', 1); $this->connections[$connection]->setOption('log_line_break', "<br/>"); } $this->connections[$connection]->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE); $this->connections[$connection]->setFetchMode(MDB2_FETCHMODE_ASSOC); if (PEAR::isError($this->connections[$connection])) { throw new Plank_Exception_Database_Connection('Error connecting ' . $connection . ': ' . $this->connections[$connection]->getMessage() . ' ' . $this->connections[$connection]->getUserInfo()); } } else { throw new Plank_Exception_Database_Config('No DSN for DB Connection ' . $connection); } } }
/** * This function is a replacement for ADONewConnection, it does some * basic additional configuration and prepares the table prefix stuff * * @param $conf usually $conf['database'] in Flyspray * @return object */ function &NewDatabase($conf = array()) { if (!is_array($conf) || extract($conf, EXTR_REFS | EXTR_SKIP) < 5) { die('Flyspray was unable to connect to the database. ' . 'Check your settings in flyspray.conf.php'); } $dbpass = rawurlencode($dbpass); if ($dbtype == 'sqlite') { $dsn = "{$dbtype}:///{$dbname}?mode=0666"; } else { $dsn = "{$dbtype}://{$dbuser}:{$dbpass}@{$dbhost}/{$dbname}?charset=utf8"; } $db =& MDB2::factory($dsn); $db->loadModule('Extended', 'x', false); if (defined('IN_UPGRADER') || defined('IN_SETUP')) { $db->loadModule('Manager'); } $dbprefix = isset($dbprefix) ? $dbprefix : ''; if ($db === false || !empty($dbprefix) && !preg_match('/^[a-z][a-z0-9_]+$/i', $dbprefix)) { die('Flyspray was unable to connect to the database. ' . 'Check your settings in flyspray.conf.php'); } define('DB_PREFIX', $dbprefix); $db->setFetchMode(MDB2_FETCHMODE_ASSOC); $db->setOption('debug', true); $db->setOption('debug_handler', '_table_prefix'); $db->setOption('quote_identifier', true); // upgrader can handle that on its own if (!defined('IN_UPGRADER') && !defined('IN_SETUP') || defined('DEBUG_SQL')) { $db->setErrorHandling(PEAR_ERROR_CALLBACK, 'show_dberror'); } return $db; }
/** * Connect to databases, but create instance not before the first time * a query gets executed. */ private function connectDbLazy() { $this->connection = MDB2::factory($this->dsn); if (PEAR::isError($this->connection)) { die("Error while lazy connecting: " . $this->connection->getMessage() . "\ndsn was: " . print_r($this->dsn) . "\n"); } }
public function myDB($host, $user, $pwd, $db, $port) { $dsn = array('phptype' => 'pgsql', 'username' => $user, 'password' => $pwd, 'hostspec' => $host, 'database' => $db, 'port' => $port); if ($this->log) { $this->writeLog(print_r($dsn, true)); } $options = array('result_buffering' => false); $this->db =& MDB2::factory($dsn, $options); //$this->db=& MDB2::connect($dsn,$options); if (!$this->db || PEAR::isError($this->db)) { if ($this->log) { $this->writeLog('Connect Error: ' . $dns); } $this->dbFehler('Connect ' . print_r($dsn, true), $this->db->getMessage()); die($this->db->getMessage()); } $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC); if ($this->log) { $this->writeLog('Connect: ok '); } $sql = 'SELECT version()'; $rs = $this->getOne($sql); if ($this->log) { $this->writeLog(print_r($rs, true)); } preg_match('/PostgreSQL\\s*([\\d]+)\\.([\\d]+)\\..*/', $rs['version'], $ver); if ($ver[1] == '9' and $ver[2] >= '3' or $ver[1] >= '10') { $this->JVer = true; } if ($this->log) { $this->writeLog(print_r($ver, true) . "!" . $this->JVer . '!'); } return $this->db; }
public function connect($dsn, $type = false, $options = false) { // Currently there are only two options: pear_mdb2 and php_native_mysql $connected = false; if ($type == 'php_native_mysql') { $this->db = mysql_connect($dsn->hostspec, $dsn->username, $dsn->password); if ($this->db) { if ($dsn->database) { mysql_select_db($dsn->database, $this->db); } $this->dsn = $dsn; $connected = true; } } else { if ($type == 'pear_mdb2') { require_once 'MDB2.php'; $this->db =& MDB2::factory($dsn, $options); if (PEAR::isError($this->db)) { $this->errors[] = $this->db->getMessage(); } else { $connected = true; $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC); } } } if (!$connected) { $errors[] = 'Unable to connect to database using type: ' . $type; return false; } else { return $this->db; } }
/** | | @init | | @description | - init | | @parameters | - | | | @return | - | **/ public function init() { //create connection debug("init() : start init [ self::{$_Dsn} ]"); //try self::$_Mdb =& MDB2::factory(self::$_Dsn, self::$_Opt); //sanity-chk if (self::err(self::$_Mdb, "init::FAILED")) { return null; } //misc options set here self::$_Mdb->setFetchMode(MDB2_FETCHMODE_ASSOC); //more options self::$_Mdb->setOption('result_buffering', true); self::$_Mdb->setOption('multi_query', true); //set here the utf-8 $utf8[] = " SET NAMES 'utf8' COLLATE utf8_unicode_ci "; $utf8[] = " SET character_set_client='utf8' "; $utf8[] = " SET character_set_connection='utf8' "; for ($i = 0; $i < @count($utf8); $i++) { $bfsql = trim($utf8[$i]); self::query($bfsql, "ERROR: {$bfsql}"); debug("init() : utf-8 [ {$bfsql} ]"); } debug("init() : done init [ self::{$_Mdb} ]"); return self::$_Mdb; }
public function __construct() { $dsn = DB_TYPE . "://" . DB_USER . ":" . DB_PASS . "@" . DB_HOST . "/" . DB_DATABASE; $this->mdb2 =& MDB2::factory($dsn); if (PEAR::isError($this->mdb2)) { die($this->mdb2->getMessage()); } }
function __construct($dsn) { $this->dbh =& MDB2::factory($dsn); if (PEAR::isError($this->dbh)) { die($this->dbh->getMessage()); } $this->dbh->setFetchMode(MDB2_FETCHMODE_ASSOC); }
function tearDown() { $db = MDB2::factory(DB_DSN); $db->query('TRUNCATE langtkursus_tilmelding_ny'); $db->query('TRUNCATE langtkursus_rate'); $db->query('TRUNCATE langtkursus_tilmelding_rate'); $db->query('TRUNCATE betaling'); }
/** * @param $dsn * @return Void */ function connect($dsn) { $this->dsn = sprintf('%1$s://%2$s:%3$s@%4$s/%5$s', 'mysql', $dsn['user'], $dsn['pass'], $dsn['host'], $dsn['name']); $this->_db =& MDB2::factory($this->dsn); if (PEAR::isError($this->_db)) { die($this->_db->getMessage()); } }
function connect() { $url = "mysql://root:@127.0.0.1/you_learn_dev"; $con = MDB2::factory($url); if (PEAR::isError($con)) { die("Error while connecting : " . $con->getMessage()); } return $con; }
function connectDB() { global $_CONF; $objConnID =& MDB2::factory($_CONF['db']['dsn']); if (MDB2::isError($objConnID)) { throw new Exception('Database connection failed: ' . $objConnID->getMessage(), SQL_CONN_ERROR); } return $objConnID; }
/** * Function fnConnectDB to connect database.Connection of the Database called on index.php * * @param array @arrDSN @see /inc/config.inc.php * @return object $hdlDb */ static function fnConnectDB($arrDSN) { # create an object of Database $hdlDb =& MDB2::factory($arrDSN); if (PEAR::isError($hdlDb)) { die($hdlDb->getMessage()); } return $hdlDb; }
function _connect() { if (!$this->_link) { $this->_link = MDB2::factory($GLOBALS['db']['database']); if (PEAR::isError($this->_link)) { internal_error("db_connect_err"); } } }
public static function main() { global $db_test, $argv, $mdburl, $skipCaseTest; $okay = true; if (array_search('--nomysql', $argv) === false) { echo "Running as MySQLSource...\n"; $db_test = new MySQLSource('localhost', "dbs_test", 'DBSTestUser', 'password', 'utf-8'); $db_test->setTimezone('America/Vancouver'); $db_test->setErrorLogging(false); $ret = PHPUnit_TextUI_TestRunner::run(self::suite(false)); $okay &= $ret->wasSuccessful(); } if (array_search('--nomdb', $argv) !== false) { return $okay; } //determine whether to use the DB driver conversion functions or our own //must be False For MySQL4. Strangely, if this is "false" when the backing DB has //a utf-8 character set the tests will still work -- the DB value will however be //corrupt (I'm not sure how to detect this) //NOTE: if "false" then the DMB2 textType will need to be set to cstring... $useDBCharset = array_search('--usedbcharset', $argv) !== false; $opts = array(); if (!$useDBCharset) { //setup as utf8 for text columns $opts['datatype_map'] = array('cstring' => 'cstring'); $opts['datatype_map_callback'] = array('cstring' => 'mdb2_cstring_utf8_callback'); //the broken combination likely means the backing store can't do case-insensitve searching properly $skipCaseTest = true; } echo "Running as MDB2DBSource...\n"; @($mdb =& MDB2::factory($mdburl, $opts)); if (@PEAR::isError($mdb)) { error_log($mdb->getMessage()); die("Unable to create MDB2 DB instance\n"); } $GLOBALS['mdb'] =& $mdb; if ($useDBCharset) { $db_test = new MDB2DBSource($mdb); } else { $db_test = new MDB2DBSource($mdb, 'cstring'); } $db_test->setErrorLogging(false); $db_test->setTimezone('Pacific/Honolulu'); if ($useDBCharset) { $res = $mdb->setCharset('utf8'); //Won't work on MySQL 4, actually the syntax of the name is DB specific it seems if (@PEAR::isError($res)) { error_log($res->getMessage()); die("Unable to set the character set\n"); } } //$mdb->loadModule( 'Extended' ); //also not needed by dbsource $ret = PHPUnit_TextUI_TestRunner::run(self::suite(true)); $okay &= $ret->wasSuccessful(); return $okay; }
/** */ function __construct() { $dsn = array('host' => 'localhost', 'name' => '', 'user' => '', 'pass' => ''); $this->dsn = sprintf('%1$s://%2$s:%3$s@%4$s/%5$s', 'mysql', $dsn['user'], $dsn['pass'], $dsn['host'], $dsn['name']); $this->_db =& MDB2::factory($this->dsn); if (PEAR::isError($this->_db)) { die($this->_db->getMessage()); } $this->_db->exec("SET NAMES 'utf8'"); }
function testConnect() { $db =& MDB2::factory($this->dsn, $this->options); if (PEAR::isError($db)) { $this->assertTrue(false, 'Connect failed bailing out - ' . $db->getMessage() . ' - ' . $db->getUserInfo()); } if (PEAR::isError($this->db)) { exit; } }
/** * The options are: * - debug_level: level of debug if it's not specified it's set by debugObject to DEBUG_DEFAULT_LEVEL * - dsn: the Data Source Name to connect to a database through PEAR::MDB2 {@link http://pear.php.net/manual/en/package.database.mdb2.intro-dsn.php}. * The DSN must be provided as an associative array or as a string. * The array format is preferred, since it doesn't require a further parsing step (see the {@link http://pear.php.net/manual/en/package.database.mdb2.intro-connect.php Connecting * chapter} for an example). The string format of the supplied DSN is in its fullest form: * phptype(dbsyntax)://username:password@protocol+hostspec/database?option=value * Examples: * $dsn = 'mysqli://*****:*****@localhost/masterdb' * $dsn = array( * 'phptype' => 'mysqli', * 'username' => 'themaster', * 'password' => 'thepowerofthepower', * 'hostspec' => 'localhost', * 'database' => 'masterdb' * ); * - mdb2_options: can contain runtime configuration settings for the MDB2 package (see the {@link http://pear.php.net/manual/en/package.database.mdb2.intro-connect.php Connecting} for more details). * - table: the name of the table where debug logs are stored. * * @param array $options * @throws BadMethodCallException * @throws exceptions */ public function __construct($options = null) { $this->_dsn = isset($options['dsn']) ? $options['dsn'] : DEBUG_DEFAULT_MYSQL_DSN; $this->_table = isset($options['table']) ? $options['table'] : DEBUG_DEFAULT_MYSQL_TABLE; $this->_mdb2_options = isset($options['mdb2_options']) ? $options['mdb2_options'] : array(); parent::__construct($options); $this->_mdb2 =& MDB2::factory($this->_dsn, $this->_mdb2_options); if (PEAR::isError($this->_mdb2)) { throw new exceptions($this->_mdb2->getMessage(), $this->_mdb2->getCode()); } }
function dbconnect() { // データベースに接続 $this->_db = MDB2::factory($this->sqltype . '://' . $this->user . ':' . $this->password . '@' . $this->server . '/' . $this->dbname . '?charset=utf8'); // 接続チェック if (MDB2::isError($this->_db)) { return $this->dbreturn(FALSE, $this->_db->getMessage()); } //カラム名でのアクセスに指定 $this->_db->setFetchMode(MDB2_FETCHMODE_ASSOC); }
function setUp() { $this->dsn = $GLOBALS['dsn']; $this->options = $GLOBALS['options']; $this->database = $GLOBALS['database']; $this->db =& MDB2::factory($this->dsn, $this->options); $this->db->setDatabase($this->database); $this->db->expectError(MDB2_ERROR_UNSUPPORTED); $this->fields = array('user_name' => 'text', 'user_password' => 'text', 'subscribed' => 'boolean', 'user_id' => 'integer', 'quota' => 'decimal', 'weight' => 'float', 'access_date' => 'date', 'access_time' => 'time', 'approved' => 'timestamp'); $this->clearTables(); }
/** * Calls MDB2::factory(). Connections are lazy loaded upon queries. * * @param array $options Array of options to pass to MDB2::factory(). Note that * you must also include the key 'dsn', which is used as * the first argument to MDB2::factory(), and is not * passed with the options argument. * * @throws OpenID_Store_Exception on error or missing DSN in options * @return void */ public function __construct(array $options) { if (!isset($options['dsn'])) { throw new OpenID_Store_Exception('Missing dsn from options'); } $dsn = $options['dsn']; unset($options['dsn']); $this->db = MDB2::factory($dsn, $options); if (PEAR::isError($this->db)) { throw new OpenID_Store_Exception('Error connecting to DB', $this->db); } }
function DBConnection() { $this->AbstractBaseComponent(); // Prepared dsn for connection $dsn = $this->db_type . '://' . $this->db_user . ':' . $this->db_pass . '@' . $this->db_host . '/' . $this->db_name; $options = array('debug' => 2, 'portability' => MDB2_PORTABILITY_ALL); // !!! $this->dbh using for all db connection. $this->dbh =& MDB2::factory($dsn, $options); if (MDB2::isError($this->dbh)) { echo $this->dbh->getMessage() . ' - ' . $this->dbh->getUserinfo(); } }
function new_MDB2_Driver_Common($c) { $options = array("debug" => 0); $db = MDB2::factory(DB_DSN, $options); if (PEAR::isError($db)) { throw new Exception($db->getMessage()); } $db->setOption("portability", MDB2_PORTABILITY_NONE); $db->setFetchMode(MDB2_FETCHMODE_ASSOC); $db->exec("SET time_zone=\"-01:00\""); $db->query('SET NAMES utf8'); return $db; }
/** * @brief connects to the database * @returns true if connection can be established or nothing (die()) * * Connects to the database as specified in config.php */ public static function connect() { // The global data we need $CONFIG_DBNAME = OC_Config::getValue("dbname", "owncloud"); $CONFIG_DBHOST = OC_Config::getValue("dbhost", ""); $CONFIG_DBUSER = OC_Config::getValue("dbuser", ""); $CONFIG_DBPASSWORD = OC_Config::getValue("dbpassword", ""); $CONFIG_DBTYPE = OC_Config::getValue("dbtype", "sqlite"); $datadir = OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data"); // do nothing if the connection already has been established if (!self::$DBConnection) { // Require MDB2.php (not required in the head of the file so we only load it when needed) require_once 'MDB2.php'; // Prepare options array $options = array('portability' => MDB2_PORTABILITY_ALL, 'log_line_break' => '<br>', 'idxname_format' => '%s', 'debug' => true, 'quote_identifier' => true); // Add the dsn according to the database type if ($CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3') { // sqlite $dsn = array('phptype' => $CONFIG_DBTYPE, 'database' => "{$datadir}/{$CONFIG_DBNAME}.db", 'mode' => '0644'); } elseif ($CONFIG_DBTYPE == 'mysql') { // MySQL $dsn = array('phptype' => 'mysql', 'username' => $CONFIG_DBUSER, 'password' => $CONFIG_DBPASSWORD, 'hostspec' => $CONFIG_DBHOST, 'database' => $CONFIG_DBNAME); } elseif ($CONFIG_DBTYPE == 'pgsql') { // PostgreSQL $dsn = array('phptype' => 'pgsql', 'username' => $CONFIG_DBUSER, 'password' => $CONFIG_DBPASSWORD, 'hostspec' => $CONFIG_DBHOST, 'database' => $CONFIG_DBNAME); } // Try to establish connection self::$DBConnection = MDB2::factory($dsn, $options); // Die if we could not connect if (PEAR::isError(self::$DBConnection)) { echo '<b>can not connect to database, using ' . $CONFIG_DBTYPE . '. (' . self::$DBConnection->getUserInfo() . ')</center>'; $error = self::$DBConnection->getMessage(); if (defined("DEBUG") && DEBUG) { error_log($error); } if (defined("DEBUG") && DEBUG) { error_log(self::$DBConnection->getUserInfo()); } die($error); } // We always, really always want associative arrays self::$DBConnection->setFetchMode(MDB2_FETCHMODE_ASSOC); //we need to function module for query pre-procesing self::$DBConnection->loadModule('Function'); } // we are done. great! return true; }