You are here

function DB::connect in Flickr API 5

Create a new DB object including a connection to the specified database

Example 1. <code> require_once 'DB.php';

$dsn = 'pgsql://user:password@host/database'; $options = array( 'debug' => 2, 'portability' => DB_PORTABILITY_ALL, );

$db =& DB::connect($dsn, $options); if (PEAR::isError($db)) { die($db->getMessage()); } </code>

@uses DB_dbase::connect(), DB_fbsql::connect(), DB_ibase::connect(), DB_ifx::connect(), DB_msql::connect(), DB_mssql::connect(), DB_mysql::connect(), DB_mysqli::connect(), DB_oci8::connect(), DB_odbc::connect(), DB_pgsql::connect(), DB_sqlite::connect(), DB_sybase::connect()

@uses DB::parseDSN(), DB_common::setOption(), PEAR::isError()

Parameters

mixed $dsn the string "data source name" or array in the: format returned by DB::parseDSN()

array $options an associative array of option names and values:

Return value

object a new DB object. A DB_Error object on failure.

File

phpFlickr/PEAR/DB.php, line 517

Class

DB
Database independent query interface

Code

function &connect($dsn, $options = []) {
  $dsninfo = DB::parseDSN($dsn);
  $type = $dsninfo['phptype'];
  if (!is_array($options)) {

    /*
     * For backwards compatibility.  $options used to be boolean,
     * indicating whether the connection should be persistent.
     */
    $options = array(
      'persistent' => $options,
    );
  }
  if (isset($options['debug']) && $options['debug'] >= 2) {

    // expose php errors with sufficient debug level
    include_once "DB/{$type}.php";
  }
  else {
    @(include_once "DB/{$type}.php");
  }
  $classname = "DB_{$type}";
  if (!class_exists($classname)) {
    $tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null, "Unable to include the DB/{$type}.php" . " file for '{$dsn}'", 'DB_Error', true);
    return $tmp;
  }
  @($obj =& new $classname());
  foreach ($options as $option => $value) {
    $test = $obj
      ->setOption($option, $value);
    if (DB::isError($test)) {
      return $test;
    }
  }
  $err = $obj
    ->connect($dsninfo, $obj
    ->getOption('persistent'));
  if (DB::isError($err)) {
    $err
      ->addUserInfo($dsn);
    return $err;
  }
  return $obj;
}