13

pdo

The configuration parameters $db_url, $db_prefix and $db_debug are defined in the file includes/db.inc.

  1. global $db_url, $db_prefix, $db_debug;
  2.  
  3. $db_url = 'mysql://username:password@localhost/databasename';
  4. $db_prefix = false;
  5. $db_debug = false;

To display a trace of all the SQL requests, set the parameter $db_debug to true and include the file trace.php from the library which defines the trace function. IMPORTANT: If you use pdo.php outside a website, include the file dump.php of the library and define the function trace as an alias of the function dump:

require_once 'dump.php';

function trace($var, $label=null) {
    return dump($var, $label);
}
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->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  28.         $db_conn->exec("SET NAMES 'utf8'");
  29.  
  30.         if ($scheme == 'mysql') {
  31.             $db_conn->exec("SET SQL_MODE='ANSI_QUOTES'");
  32.         }
  33.  
  34.         $db_scheme=$scheme;
  35.     }
  36.     catch (PDOException $e) {
  37.         die($e->getMessage());
  38.     }
  39.  
  40.     return $db_conn;
  41. }

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.     return $rows;
  23. }

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

register

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].