You are here

function DB_mysql::connect in Flickr API 5

Connect to the database server, log in and open the database

Don't call this method directly. Use DB::connect() instead.

PEAR DB's mysql driver supports the following extra DSN options: + new_link If set to true, causes subsequent calls to connect() to return a new connection link instead of the existing one. WARNING: this is not portable to other DBMS's. Available since PEAR DB 1.7.0. + client_flags Any combination of MYSQL_CLIENT_* constants. Only used if PHP is at version 4.3.0 or greater. Available since PEAR DB 1.7.0.

Parameters

array $dsn the data source name:

bool $persistent should the connection be persistent?:

Return value

int DB_OK on success. A DB_Error object on failure.

File

phpFlickr/PEAR/DB/mysql.php, line 192

Class

DB_mysql
The methods PEAR DB uses to interact with PHP's mysql extension for interacting with MySQL databases

Code

function connect($dsn, $persistent = false) {
  if (!PEAR::loadExtension('mysql')) {
    return $this
      ->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  }
  $this->dsn = $dsn;
  if ($dsn['dbsyntax']) {
    $this->dbsyntax = $dsn['dbsyntax'];
  }
  $params = array();
  if ($dsn['protocol'] && $dsn['protocol'] == 'unix') {
    $params[0] = ':' . $dsn['socket'];
  }
  else {
    $params[0] = $dsn['hostspec'] ? $dsn['hostspec'] : 'localhost';
    if ($dsn['port']) {
      $params[0] .= ':' . $dsn['port'];
    }
  }
  $params[] = $dsn['username'] ? $dsn['username'] : null;
  $params[] = $dsn['password'] ? $dsn['password'] : null;
  if (!$persistent) {
    if (isset($dsn['new_link']) && ($dsn['new_link'] == 'true' || $dsn['new_link'] === true)) {
      $params[] = true;
    }
    else {
      $params[] = false;
    }
  }
  if (version_compare(phpversion(), '4.3.0', '>=')) {
    $params[] = isset($dsn['client_flags']) ? $dsn['client_flags'] : null;
  }
  $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';
  $ini = ini_get('track_errors');
  $php_errormsg = '';
  if ($ini) {
    $this->connection = @call_user_func_array($connect_function, $params);
  }
  else {
    ini_set('track_errors', 1);
    $this->connection = @call_user_func_array($connect_function, $params);
    ini_set('track_errors', $ini);
  }
  if (!$this->connection) {
    if (($err = @mysql_error()) != '') {
      return $this
        ->raiseError(DB_ERROR_CONNECT_FAILED, null, null, null, $err);
    }
    else {
      return $this
        ->raiseError(DB_ERROR_CONNECT_FAILED, null, null, null, $php_errormsg);
    }
  }
  if ($dsn['database']) {
    if (!@mysql_select_db($dsn['database'], $this->connection)) {
      return $this
        ->mysqlRaiseError();
    }
    $this->_db = $dsn['database'];
  }
  return DB_OK;
}