<?php
#################################################################
# A library of universal database functions for PHP 4.1.0+
# Version 2.00, 8.V.2006
# For now supports MySQL and PostgreSQL
# (c) Ansis Ataols Bērziņš <ataols@latnet.lv>
# http://ansis.lv/unidb/
# Freeware - can be used and distributed free of charge.
# Contains only functions already needed, if you need some more,
# just drop me a message.
#
# To use, just insert in your script two lines:
# include_once './unidb.php';
# $UNIDB = 'POSTGRE'; # (or 'MYSQL')
#
# Currently supported functions:
# unidb_affected_rows
# unidb_close
# unidb_connect
# unidb_data_seek
# unidb_error
# unidb_fetch_array
# unidb_fetch_assoc
# unidb_fetch_object
# unidb_fetch_row
# unidb_field_len
# unidb_field_name
# unidb_field_type
# unidb_free_result
# unidb_insert_id
# unidb_num_fields
# unidb_num_rows
# unidb_query
# unidb_begin_transaction
# unidb_commit_transaction
# unidb_rollback_transaction
#################################################################

# a list of databases we are able to use
$unidbs = array('POSTGRE','MYSQL');
# for the selection of the database type include the variable $UNIDB at
# the beginning of your script, for example, "$UNIDB = 'POSTGRE';"

# unidb_affected_rows - returns number of affected records
# input:    $qres - query result resource
# output:   number of affected records
#            0 or -1 in some special cases
function unidb_affected_rows($qres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_affected_rows($qres);
      break;
    case 
'MYSQL':
      
$result mysql_affected_rows();
      break;
  }
  return 
$result;
# unidb_affected_rows


# unidb_close - closes a connection to a database
# input:    $conres - connection resource link identifier
# output:   TRUE, if successfull
#            FALSE, if unsuccessfull
function unidb_close($conres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
        
$result pg_close($conres);
      break;
    case 
'MYSQL':
        
$result mysql_close($conres);
      break;
  }
  return 
$result;
# unidb_close


# unidb_connect - makes connection to a database
# input:    $hostname - hostname of the database server
#            $username - database user's username
#            $password - database user's password
#            $database - the database should be opened
# output:   connection resource link identifier, if successfull
#            FALSE, if unsuccessfull
function unidb_connect($hostname$username$password$database) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      if (
$hostname == 'localhost') {
          
$conres pg_connect("dbname=$database user=$username password=$password");
#        echo "dbname=$database user=$username password=$password<BR>";
      
} else {
          
$conres pg_connect("host=$hostname dbname=$database user=$username password=$password");
#        echo "host=$hostname dbname=$database user=$username password=$password<BR>";
      
}
      break;
    case 
'MYSQL':
      
$conres mysql_connect($hostname$username$password);
      if (!
mysql_select_db($database$conres)) $conres FALSE;
      break;
  }
  return 
$conres;
# unidb_connect


# unidb_data_seek - move internal result pointer
# input:    $qres - query result resource
#             $offset - the row number move to (numbered from 0)
# output:   TRUE on success
#            FALSE on failure
function unidb_data_seek($qres$offset) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_result_seek($qres,$offset);
      break;
    case 
'MYSQL':
      
$result mysql_data_seek($qres,$offset);
      break;
  }
  return 
$result;
# unidb_data_seek


# unidb_error - returns the last error message
# input:    $conres - connection resource link identifier (optional)
# output:   error message string
#            an empty string, if no error occured
function unidb_error() {
  if (
func_num_args() > 0) { $conres func_get_arg(0); }
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result = isset($conres) ? pg_last_error($conres) : pg_last_error();
      break;
    case 
'MYSQL':
      
$result = isset($conres) ? mysql_error($conres) : mysql_error();
      break;
  }
  return 
$result;
# unidb_error


# unidb_fetch_array - fetches a row as an array (row by row)
# input:    $qres - query result resource
# output:   array that corresponds to the fetched row
#            FALSE, if there are no more rows
function unidb_fetch_array($qres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_fetch_array($qres);
      break;
    case 
'MYSQL':
      
$result mysql_fetch_array($qres);
      break;
  }
  return 
$result;
# unidb_fetch_array


# unidb_fetch_assoc - fetches a row as an associative array (row by row)
# input:    $qres - query result resource
# output:   object that corresponds to the fetched row
#            FALSE, if there are no more rows
function unidb_fetch_assoc($qres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_fetch_assoc($qres);
      break;
    case 
'MYSQL':
      
$result mysql_fetch_assoc($qres);
      break;
  }
  return 
$result;
# unidb_fetch_assoc


# unidb_fetch_object - fetches a row as an object (row by row)
# input:    $qres - query result resource
# output:   object that corresponds to the fetched row
#            FALSE, if there are no more rows
function unidb_fetch_object($qres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_fetch_object($qres);
      break;
    case 
'MYSQL':
      
$result mysql_fetch_object($qres);
      break;
  }
  return 
$result;
# unidb_fetch_object


# unidb_fetch_row - fetches a row as an enumerated array (row by row)
# input:    $qres - query result resource
# output:   array that corresponds to the fetched row
#            FALSE, if there are no more rows
function unidb_fetch_row($qres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_fetch_row($qres);
      break;
    case 
'MYSQL':
      
$result mysql_fetch_row($qres);
      break;
  }
  return 
$result;
# unidb_fetch_row


# unidb_field_len - get the field length
# input:    $qres - query result resource
#             $offset - the field offset
# output:   string - field type, or FALSE on error
function unidb_field_len($qres$offset) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_field_size($qres,$offset);
      break;
    case 
'MYSQL':
      
$result mysql_field_len($qres,$offset);
      break;
  }
  return 
$result;
# unidb_field_len


# unidb_field_name - get the field name
# input:    $qres - query result resource
#             $offset - the field offset
# output:   string - field name, or FALSE on error
function unidb_field_name($qres$offset) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_field_name($qres,$offset);
      break;
    case 
'MYSQL':
      
$result mysql_field_name($qres,$offset);
      break;
  }
  return 
$result;
# unidb_field_name


# unidb_field_type - get the field type
# input:    $qres - query result resource
#             $offset - the field offset
# output:   string - field type, or FALSE on error
function unidb_field_type($qres$offset) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_field_type($qres,$offset);
      break;
    case 
'MYSQL':
      
$result mysql_field_type($qres,$offset);
      break;
  }
  return 
$result;
# unidb_field_type


# unidb_free_result - free result memory
# input:    $qres - query result resource
# output:   TRUE on success or FALSE on failure
function unidb_free_result($qres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_free_result($qres);
      break;
    case 
'MYSQL':
      
$result mysql_free_result($qres);
      break;
  }
  return 
$result;
# unidb_free_result


# unidb_insert_id - gets the ID generated at the previous INSERT operation
# input:    $conres - connection resource link identifier
#            $tablename - name of the table where data inserted (PostgreSQL only)
#            $serialname - name of the serial field (PostgreSQL only)
# output:   the identifier
#
function unidb_insert_id($conres$tablename$serialname) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$sql "SELECT currval('".$tablename.'_'.$serialname."_seq')";
      
$result pg_query($conres$sql);
      if (!
$result) {
        echo 
"Error ($tablename): ".unidb_error()."<BR>\n$sql<BR>\n";
      } else {
        
$seq_array pg_fetch_row($result0);
        
$insid $seq_array[0];
      }
      break;
    case 
'MYSQL':
      
$insid mysql_insert_id($conres);
      break;
  }
  return 
$insid;
# unidb_insert_id


# unidb_num_fields - get number of fields in result
# input:    $qres - query result resource
# output:   number of fields success or FALSE on failure
function unidb_num_fields($qres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_num_fields($qres);
      
$result = ($result >=$result FALSE);
      break;
    case 
'MYSQL':
      
$result mysql_num_fields($qres);
      break;
  }
  return 
$result;
# unidb_num_fields


# unidb_num_rows - get number of rows in result
# input:    $qres - query result resource
# output:   number of fields success or FALSE on failure
function unidb_num_rows($qres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$result pg_num_rows($qres);
      
$result = ($result >=$result FALSE);
      break;
    case 
'MYSQL':
      
$result mysql_num_rows($qres);
      break;
  }
  return 
$result;
# unidb_num_rows


# unidb_query - makes a database query
# input:    $query - query
#            $conres - connection resource link identifier
# output:   TRUE, if successfull
#            FALSE, if unsuccessfull
function unidb_query($query$conres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$qres pg_query($conres$query);
      break;
    case 
'MYSQL':
        
$qres mysql_query($query$conres);
      break;
  }
  return 
$qres;
# unidb_query


# unidb_begin_transaction - starts a transaction
# input:    $conres - connection resource link identifier
# output:   the query result
#
function unidb_begin_transaction($conres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$sql "BEGIN";
      
$result pg_query($conres$sql);
      if (!
$result) {
        echo(
'Error: '.unidb_error()."<BR>\n$sql<BR>\n");
      }
      return 
$result;
    case 
'MYSQL':
      
$sql "BEGIN";
      
$result mysql_query($sql$conres);
      if (!
$result) {
        echo(
'Error: '.unidb_error()."<BR>\n$sql<BR>\n");
      }
      return 
$result;
  }
# unidb_begin_transaction


# unidb_commit_transaction - ends a transaction
# input:    $conres - connection resource link identifier
# output:   the query result
#
function unidb_commit_transaction($conres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$sql "COMMIT";
      
$result pg_query($conres$sql);
      if (!
$result) {
        echo(
'Error: '.unidb_error()."<BR>\n$sql<BR>\n");
      }
      return 
$result;
    case 
'MYSQL':
      
$sql "COMMIT";
      
$result mysql_query($sql$conres);
      if (!
$result) {
        echo(
'Error: '.unidb_error()."<BR>\n$sql<BR>\n");
      }
      return 
$result;
  }
# unidb_commit_transaction


# unidb_rollback_transaction - cancels a transaction
# input:    $conres - connection resource link identifier
# output:   the query result
#
function unidb_rollback_transaction($conres) {
  global 
$UNIDB;
  switch (
$UNIDB) {
    case 
'POSTGRE':
      
$sql "ROLLBACK";
      
$result pg_query($conres$sql);
      if (!
$result) {
        echo(
'Error: '.unidb_error()."<BR>\n$sql<BR>\n");
      }
      return 
$result;
    case 
'MYSQL':
      
$sql "ROLLBACK";
      
$result mysql_query($sql$conres);
      if (!
$result) {
        echo(
'Error: '.unidb_error()."<BR>\n$sql<BR>\n");
      }
      return 
$result;
  }
# unidb_rollback_transaction
?>