<?php
function db_generic_connect () {
GLOBAL $db_server_address, $db_name, $db_login, $db_password;
//$db_link = mysql_connect ($db_server_address, $db_login , $db_password) OR DIE ("Connection Not Made to DB");
//mysql_select_db ($db_name, $db_link) OR DIE ("DB Not Selected");
//$db_link = pg_Connect ("host=".$db_server_address." dbname=".$db_name." user=".$db_login." password=".$db_password."\"");
$connect_string = 'user='.$db_login.' password='.$db_password." port=5432 dbname='".$db_name."'";
//echo $connect_string;
//$db_link = pg_Connect ("user=postgres password= host=localhost port=5432 dbname=".$db_name);
$db_link = pg_Connect ($connect_string);
return $db_link;
}
function run_query ($sql, $error_message) {
GLOBAL $db_link, $transaction_in_progress;
//echo "<br/>_________________<br/><span class=\"smaller75\">Query: ".$error_message.'</span>'; echo '<pre>'.$sql.'</pre>'; $timer = new Script_timer();
$query_result = pg_exec ($db_link, $sql);
if(!$query_result) {
if ( $transaction_in_progress ) {
transaction_rollback ('Failed query caused rollback of transaction<p/>');
}
echo 'DB SQL Query Error: '.$error_message.' '.getenv('SCRIPT_NAME').' '.$sql.'<p/>';
exit;
}
//$ex_time = $timer->Get_Time(4); echo "<span class=\"smaller75\">Seconds to execute: ".$ex_time.' -- Grand total seconds: '.$timer->total_time.'</span>';
return $query_result;
}
function fetch_array ($query_result, $error_message, $row_num='5432') {
//$row_array = mysql_fetch_array ($query_result);
$row_array = pg_fetch_array ($query_result, $row_num);
if(!$row_array) {echo 'DB Fetch Array Error: '.$error_message.' '.getenv('SCRIPT_NAME'); exit; }
return $row_array;
if($row_num='5432'){echo 'No ROW number<br/>';}
}
function num_rows ($query_result) {
//$row_count = mysql_num_rows ($query_result);
$row_count = pg_numrows ($query_result);
return $row_count;
}
function fetch_result ($query_result, $error_message) {
//$row_result_raw = mysql_result ($query_result, 0);
$row_result_raw = pg_fetch_array ($query_result, 0);
if(!$row_result_raw) {echo 'DB Fetch Result Error: '.$error_message.' '.getenv('SCRIPT_NAME'); exit; }
//$row_result = $row_result_raw; //MySQL only
$row_result = $row_result_raw[0];//postgreSQL only
return $row_result;
}
function fetch_assoc ($query_result, $error_message, $row_num="5432") {
//$row_array = mysql_fetch_assoc ($query_result);
$row_array = pg_fetch_array ($query_result, $row_num, "PGSQL_ASSOC");
if(!$row_array) {echo "DB Fetch Array Error: ".$error_message." ".getenv("SCRIPT_NAME"); exit; }
return $row_array;
if($row_num="5432"){echo "No ROW number<br/>";}
}
function run_query_return_array ($sql, $error_message, $use_array_key="") {
GLOBAL $db_link;
//echo $sql."<p/>";
$query_result = pg_exec ($db_link, $sql);
if(!$query_result) {echo 'DB SQL run_query_return_array Error: '.$error_message.'
'.getenv('SCRIPT_NAME').' '.$sql; exit; }
$row_count = pg_numrows ($query_result);
//If a key is specified, use it to assemble the array to be returned
if($use_array_key) {
for ( $i = 0; $i < $row_count; $i++ ) {
$temp_return_array = pg_fetch_array ($query_result, $i);
$return_array[$temp_return_array[$use_array_key]] = $temp_return_array;
}
}
else {
for ( $i = 0; $i < $row_count; $i++ ) {
$return_array[$i] = pg_fetch_array ($query_result, $i);
}
}
return $return_array;
}
function run_query_return_single_row ($sql, $error_message) {
GLOBAL $db_link;
//echo $sql."<p/>";
$query_result = pg_exec ($db_link, $sql);
if(!$query_result) {echo 'DB SQL run_query_return_array Error: '.$error_message.'
'.getenv('SCRIPT_NAME').' '.$sql; exit; }
if (pg_numrows ($query_result) > 0) {
$return_array = pg_fetch_array ($query_result, 0);
}
return $return_array;
}
function return_column_names_array ($table_name, $offset=0) {
$result = run_query("SELECT attname FROM pg_attribute, pg_type WHERE typrelid=attrelid AND typname = '".$table_name."'",
"Column Names Not Retreived from ".$table_name);
$row_count = num_rows ($result);
settype($returned_array["array"], "array");
for($i=7+$offset; $i < $row_count; $i++) {
$raw_array = fetch_array($result, "Column name in table ".$table_name." not fetched", $i);
$first_array_result = $raw_array[0];
//if ( $first_array_result != 'report_type_id' ) {
$returned_array["list"] .= $first_array_result;
if ($i != $row_count-1) {
$returned_array["list"] .= ", ";
}
array_push($returned_array["array"], $first_array_result);
//}
}
$returned_array["column_count"] = count($returned_array["array"]);
return $returned_array;
}
function transaction_begin ( $error_message ) {
GLOBAL $transaction_in_progress, $transaction_in_progress_count;
if ( strlen ( $transaction_in_progress ) > 0 ) {
//echo 'Transaction added to: '.$error_message.' already in progress<p/>';
$transaction_in_progress = $transaction_in_progress.", ".$error_message;
$transaction_in_progress_count++;
}
else {
run_query ( 'BEGIN', $error_message );
//echo 'NEW Begin'.$error_message.'<br/>';
$transaction_in_progress = $error_message;
$transaction_in_progress_count = 1;
}
}
function transaction_verify ( $error_message ) {
GLOBAL $transaction_in_progress;
if ( !$transaction_in_progress ) {
//echo 'There is no transaction encompassing: '.$error_message.'<p/>';
$ret_val = -1;
}
else {
$ret_val = 1;
}
return $ret_val;
}
function transaction_rollback ($error_message) {
GLOBAL $transaction_in_progress;
//echo 'The following transaction rolled back: '.$transaction_in_progress.'; Error message:
//'.$error_message.'<p/>';;
run_query ( 'ROLLBACK', 'Error rolling back: '.$transaction_in_progress );
$transaction_in_progress = '';
$transaction_in_progress_count = '';
}
function transaction_commit () {
GLOBAL $transaction_in_progress, $transaction_in_progress_count;
//echo 'Commit'.$transaction_in_progress.'<br/>';
$transaction_in_progress_count--;
if ( $transaction_in_progress_count == 0 ) {
run_query ( 'COMMIT', $error_message );
//unset ( $transaction_in_progress );
$transaction_in_progress = '';
$transaction_in_progress_count = 0;
//echo 'ACUTAL Commit'.$transaction_in_progress.'<br/>';
}
}
class Script_Timer {
var $start_timea;
var $total_time;
function Script_Timer ()
{
$z = explode (' ', microtime());
$z = $z[1] + $z[0];
define ('TIMER_START_TIME', $z);
$this->start_timea = microtime ();
return $start_time;
}
function Get_Time ($decimals=2)
{
// $decimals will set the number of decimals you want for your milliseconds.
// format start time
$start_time = explode (' ', $this->start_timea);
$start_time = $start_time[1] + $start_time[0];
// get and format end time
$end_time = explode (' ', microtime());
$end_time = $end_time[1] + $end_time[0];
$this->total_time = number_format ($end_time - TIMER_START_TIME, $decimals);
$h = $end_time - $start_time;
$i = number_format ($h, $decimals);
if ( $h > 1 ) {
$i = "<span class=\"headline\">".$i."</span>";
}
return $i;
}
}
?>