10

pdo

db_connect
SYNOPSIS

db_connect($url)

DESCRIPTION

The connector to a database has the format scheme://username:password@localhost/databasename. scheme is either mysql for MySQL or pgsql for PostgreSQL.

CODE
  1. $db_conn=false;
  2. $db_scheme=false;
  3.  
  4. function db_connect($url, $persistent=true) {
  5.     global $db_conn, $db_scheme;
  6.  
  7.     $url = parse_url($url);
  8.  
  9.     $scheme = $url['scheme'];
  10.     $host = urldecode($url['host']);
  11.     if (isset($url['port'])) {
  12.         $host = $host . ':' . $url['port'];
  13.     }
  14.     $user = urldecode($url['user']);
  15.     $pass = isset($url['pass']) ? urldecode($url['pass']) : '';
  16.     $path = urldecode($url['path']);
  17.     if ($path[0] == '/') {
  18.         $path = substr($path, 1);
  19.     }
  20.  
  21.     $dsn = "$scheme:host=$host;dbname=$path";
  22.     $options = array(PDO::ATTR_PERSISTENT => $persistent ? true : false);
  23.  
  24.     try {
  25.         $db_conn = new PDO($dsn, $user, $pass, $options);
  26.         $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  27.         $db_conn->exec("SET NAMES 'utf8'");
  28.  
  29.         if ($scheme == 'mysql') {
  30.             $db_conn->exec("SET SQL_MODE='ANSI_QUOTES'");
  31.         }
  32.  
  33.         $db_scheme=$scheme;
  34.     }
  35.     catch (PDOException $e) {
  36.         die($e->getMessage());
  37.     }
  38.  
  39.     return $db_conn;
  40. }

db_connect parses $url to extract the parameters for the connection to the DB then opens a persistent connection with the server and selects the requested DB. The dialog is configured in UTF-8. In case of a problem, db_connect pulls up the MySQL or the PostgreSQL error and triggers a PHP error. Note that $db_conn is a global variable.

  1. function db_close() {
  2.     global $db_conn;
  3.  
  4.     $db_conn=null;
  5. }
  1. function db_version() {
  2.     global $db_conn;
  3.  
  4.     $r = $db_conn->getAttribute(PDO::ATTR_SERVER_VERSION);
  5.  
  6.     return $r;
  7. }
  1. function db_query($sql) {
  2.     global $db_debug;
  3.     global $db_conn;
  4.  
  5.     if ($db_debug) {
  6.         trace($sql);
  7.     }
  8.  
  9.     try {
  10.         $r = $db_conn->query($sql);
  11.     }
  12.     catch (PDOException $e) {
  13.         die($e->getMessage());
  14.     }
  15.  
  16.     $rows = $r->fetchAll(PDO::FETCH_ASSOC);
  17.  
  18.     if (!$rows) {
  19.         return false;
  20.     }
  21.  
  22.     if (get_magic_quotes_runtime()) {
  23.         foreach ($rows as $row) {
  24.             foreach ($row as $k => &$v) {
  25.                 $v = stripslashes($v);
  26.             }
  27.         }
  28.     }
  29.  
  30.     return $rows;
  31. }

db_query executes the request $sql and returns all the response lines in an array or false if the request didn't return anything.

  1. function db_insert($sql) {
  2.     return _db_sql_exec($sql);
  3. }
  1. function db_update($sql) {
  2.     return _db_sql_exec($sql);
  3. }
  1. function db_delete($sql) {
  2.     return _db_sql_exec($sql);
  3. }
  1. function db_exec($sql) {
  2.     return _db_sql_exec($sql);
  3. }
  1. function db_insert_id($id=null) {
  2.     global $db_conn;
  3.  
  4.     $r = $db_conn->lastInsertId($id);
  5.  
  6.     return $r;
  7. }
  1. function db_sql_arg($s, $escape=true, $optional=false) {
  2.     global $db_conn;
  3.  
  4.     if ($s === NULL or $s === false or $s === '') {
  5.         return $optional ? 'NULL' : "''";
  6.     }
  7.  
  8.     return $escape ? $db_conn->quote($s) : "'$s'";
  9. }
  1. function db_prefix_table($table) {
  2.     global $db_prefix;
  3.  
  4.     return $db_prefix ? $db_prefix . $table : $table;
  5. }
  1. function _db_sql_exec($sql) {
  2.     global $db_debug;
  3.     global $db_conn;
  4.  
  5.     if ($db_debug) {
  6.         trace($sql);
  7.     }
  8.  
  9.     try {
  10.         $r = $db_conn->exec($sql);
  11.     }
  12.     catch (PDOException $e) {
  13.         die($e->getMessage());
  14.     }
  15.  
  16.     return $r;
  17. }
SEE ALSO

db, readarg

Comments

To add a comment, click here.