pdo
The configuration parameters $db_url
, $db_prefix
and $db_debug
are defined in the file includes/db.inc.
- global $db_url, $db_prefix, $db_debug;
- $db_url = 'mysql://username:password@localhost/databasename';
- $db_prefix = false;
- $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
:
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
- $db_conn=false;
- $db_scheme=false;
- function db_connect($url, $persistent=true) {
- global $db_conn, $db_scheme;
- $url = parse_url($url);
- $scheme = $url['scheme'];
- $host = urldecode($url['host']);
- if (isset($url['port'])) {
- $host = $host . ':' . $url['port'];
- }
- $user = urldecode($url['user']);
- $pass = isset($url['pass']) ? urldecode($url['pass']) : '';
- $path = urldecode($url['path']);
- if ($path[0] == '/') {
- $path = substr($path, 1);
- }
- $dsn = "$scheme:host=$host;dbname=$path";
- $options = array(PDO::ATTR_PERSISTENT => $persistent ? true : false);
- try {
- $db_conn = new PDO($dsn, $user, $pass, $options);
- $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $db_conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- $db_conn->exec("SET NAMES 'utf8'");
- if ($scheme == 'mysql') {
- $db_conn->exec("SET SQL_MODE='ANSI_QUOTES'");
- }
- $db_scheme=$scheme;
- }
- catch (PDOException $e) {
- die($e->getMessage());
- }
- return $db_conn;
- }
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.
- function db_close() {
- global $db_conn;
- $db_conn=null;
- }
- function db_version() {
- global $db_conn;
- $r = $db_conn->getAttribute(PDO::ATTR_SERVER_VERSION);
- return $r;
- }
- function db_query($sql) {
- global $db_debug;
- global $db_conn;
- if ($db_debug) {
- trace($sql);
- }
- try {
- $r = $db_conn->query($sql);
- }
- catch (PDOException $e) {
- die($e->getMessage());
- }
- $rows = $r->fetchAll(PDO::FETCH_ASSOC);
- if (!$rows) {
- return false;
- }
- return $rows;
- }
db_query
executes the request $sql
and returns all the response lines in an array or false
if the request didn't return anything.
- function db_insert($sql) {
- return _db_sql_exec($sql);
- }
- function db_update($sql) {
- return _db_sql_exec($sql);
- }
- function db_delete($sql) {
- return _db_sql_exec($sql);
- }
- function db_exec($sql) {
- return _db_sql_exec($sql);
- }
- function db_insert_id($id=null) {
- global $db_conn;
- $r = $db_conn->lastInsertId($id);
- return $r;
- }
- function db_sql_arg($s, $escape=true, $optional=false) {
- global $db_conn;
- if ($s === NULL or $s === false or $s === '') {
- return $optional ? 'NULL' : "''";
- }
- return $escape ? $db_conn->quote($s) : "'$s'";
- }
- function db_prefix_table($table) {
- global $db_prefix;
- return $db_prefix ? $db_prefix . $table : $table;
- }
- function _db_sql_exec($sql) {
- global $db_debug;
- global $db_conn;
- if ($db_debug) {
- trace($sql);
- }
- try {
- $r = $db_conn->exec($sql);
- }
- catch (PDOException $e) {
- die($e->getMessage());
- }
- return $r;
- }
Comments