function connect() { if (strcmp($this->database_type, "oracle") == 0) { // do an oracle connect } if (strcmp($this->database_type, "mysql") == 0) { $connection = mysql_connect($this->host, $this->user, $this->pass); if (!$connection) { die("Could not connect: " . mysql_error()); } if (!mysql_select_db($this->database, $connection)) { die("Could not select: " . mysql_error()); } $this->connection = $connection; } if (strcmp($this->database_type, "drizzle") == 0) { $drizzle = drizzle_create(); try { $this->connection = drizzle_con_add_tcp($drizzle, $this->host, $this->port, $this->user, $this->pass, $this->database, 0); if ($this->connection == FALSE) { throw new Exception('drizzle_con_add_tcp_error'); } } catch (Exception $e) { echo "Caught active server error exception:\n", $e->getMessage(), " ", $e->getFile(), ": Line ", $e->getLine(), "\n", $e->getTraceAsString(), "\n"; } } }
function bmark_connect($database_type = 'mysql') { $Data = Spyc::YAMLLoad(BMARK . 'db.yaml'); if ($database_type == 'mysql') { $user = $Data['mysqld']['user']; $pass = $Data['mysqld']['pass']; $host = $Data['mysqld']['host']; $dbname = $Data['mysqld']['dbname']; // trying connect try { $dbh = mysql_connect("{$host}", "{$user}", "{$pass}"); if ($dbh == FALSE) { throw new Exception('mysql_connect_error'); } } catch (Exception $e) { echo "Caught active server error exception:\n", $e->getMessage(), " ", $e->getFile(), ": Line ", $e->getLine(), "\n", $e->getTraceAsString(), "\n"; } // trying db select try { if (!mysql_select_db($dbname, $dbh)) { throw new Exception('mysql_select_db_eror'); } } catch (Exception $e) { echo "({$dbname}) Caught active server error exception:\n", $e->getMessage(), " ", $e->getFile(), ": Line ", $e->getLine(), "\n", $e->getTraceAsString(), "\n"; } } if ($database_type == 'drizzle') { // set connection parameters $host = $Data['drizzled']['host']; $port = $Data['drizzled']['port']; $user = $Data['drizzled']['user']; $pass = $Data['drizzled']['pass']; $db = $Data['drizzled']['dbname']; // create drizzle object $drizzle = drizzle_create(); // connect to database server try { $dbh = drizzle_con_add_tcp($drizzle, $host, $port, $user, $pass, $db, 0); if ($dbh == FALSE) { throw new Exception('drizzle_con_add_tcp_error'); } } catch (Exception $e) { echo "Caught active server error exception:\n", $e->getMessage(), " ", $e->getFile(), ": Line ", $e->getLine(), "\n", $e->getTraceAsString(), "\n"; } } # print_r($dbh); return $dbh; }
/** * Test database connection * * @param string $extension 'drizzle', 'mysql' or 'mysqli' * @param string $connect_type 'tcp' or 'socket' * @param string $host * @param string $port * @param string $socket * @param string $user * @param string $pass * @param string $error_key * * @return bool|array */ function test_db_connection($extension, $connect_type, $host, $port, $socket, $user, $pass = null, $error_key = 'Server') { // test_php_errormsg(); $socket = empty($socket) || $connect_type == 'tcp' ? null : $socket; $port = empty($port) || $connect_type == 'socket' ? null : ':' . $port; $error = null; if ($extension == 'drizzle') { while (1) { $drizzle = @drizzle_create(); if (!$drizzle) { $error = __('Could not initialize Drizzle connection library'); break; } $conn = $socket ? @drizzle_con_add_uds($socket, $user, $pass, null, 0) : @drizzle_con_add_tcp($drizzle, $host, $port, $user, $pass, null, 0); if (!$conn) { $error = __('Could not connect to Drizzle server'); drizzle_free($drizzle); break; } // connection object is set up but we have to send some query // to actually connect $res = @drizzle_query($conn, 'SELECT 1'); if (!$res) { $error = __('Could not connect to Drizzle server'); } else { drizzle_result_free($res); } drizzle_con_free($conn); drizzle_free($drizzle); break; } } else { if ($extension == 'mysql') { $conn = @mysql_connect($host . $socket . $port, $user, $pass); if (!$conn) { $error = __('Could not connect to MySQL server'); } else { mysql_close($conn); } } else { $conn = @mysqli_connect($host, $user, $pass, null, $port, $socket); if (!$conn) { $error = __('Could not connect to MySQL server'); } else { mysqli_close($conn); } } } // test_php_errormsg(false); if (isset($php_errormsg)) { $error .= " - {$php_errormsg}"; } return is_null($error) ? true : array($error_key => $error); }
/** * Test database connection * * @param string $connect_type 'tcp' or 'socket' * @param string $host host name * @param string $port tcp port to use * @param string $socket socket to use * @param string $user username to use * @param string $pass password to use * @param string $error_key key to use in return array * * @return bool|array */ public static function testDBConnection($connect_type, $host, $port, $socket, $user, $pass = null, $error_key = 'Server') { // static::testPHPErrorMsg(); $error = null; if (PMA_DatabaseInterface::checkDbExtension('mysqli')) { $socket = empty($socket) || $connect_type == 'tcp' ? null : $socket; $port = empty($port) || $connect_type == 'socket' ? null : $port; $extension = 'mysqli'; } else { $socket = empty($socket) || $connect_type == 'tcp' ? null : ':' . ($socket[0] == '/' ? '' : '/') . $socket; $port = empty($port) || $connect_type == 'socket' ? null : ':' . $port; $extension = 'mysql'; } // dead code (drizzle extension) if ($extension == 'drizzle') { while (1) { $drizzle = @drizzle_create(); if (!$drizzle) { $error = __('Could not initialize Drizzle connection library!'); break; } $conn = $socket ? @drizzle_con_add_uds($socket, $user, $pass, null, 0) : @drizzle_con_add_tcp($drizzle, $host, $port, $user, $pass, null, 0); if (!$conn) { $error = __('Could not connect to the database server!'); drizzle_free($drizzle); break; } // connection object is set up but we have to send some query // to actually connect $res = @drizzle_query($conn, 'SELECT 1'); if (!$res) { $error = __('Could not connect to the database server!'); } else { drizzle_result_free($res); } drizzle_con_free($conn); drizzle_free($drizzle); break; } } else { if ($extension == 'mysql') { $conn = @mysql_connect($host . $port . $socket, $user, $pass); if (!$conn) { $error = __('Could not connect to the database server!'); } else { mysql_close($conn); } } else { $conn = @mysqli_connect($host, $user, $pass, null, $port, $socket); if (!$conn) { $error = __('Could not connect to the database server!'); } else { mysqli_close($conn); } } } // static::testPHPErrorMsg(false); if (isset($php_errormsg)) { $error .= " - {$php_errormsg}"; } return is_null($error) ? true : array($error_key => $error); }
/** * This function opens a connection using the data source provided. * * @access public * @override * @throws Throwable_Database_Exception indicates that there is problem with * opening the connection * * @see http://wiki.drizzle.org/MySQL_Differences */ public function open() { if (!$this->is_connected()) { $handle = drizzle_create(); $host = $this->data_source->host; $port = $this->data_source->port; $database = $this->data_source->database; $username = $this->data_source->username; $password = $this->data_source->password; $this->resource = @drizzle_con_add_tcp($handle, $host, $port, $username, $password, $database, 0); if ($this->resource === FALSE) { throw new Throwable_Database_Exception('Message: Failed to establish connection. Reason: :reason', array(':reason' => @drizzle_error($handle))); } // "There is no CHARSET or CHARACTER SET commands, everything defaults to UTF-8." } }