<?php	// TWIG		PostGreSQL Functions

 Function SQLString ($string) {		// Fix SQL Strings
  $string = addslashes($string);
  return $string;
 }

 Function MakeInsertString ($data) {
  while ( list( $key, $val ) = each( $data ) ) {
   if ($key && $val) {
    $string["fields"] .= "$key, ";
    $string["values"] .= "'" . SQLString($val) . "', ";
   }
  }
  $string["fields"]=ereg_replace(", $","",$string["fields"]);
  $string["values"]=ereg_replace(", $","",$string["values"]);
  return $string;
 }

 Function MakeUpdateString ($data) {
  while ( list( $key, $val ) = each( $data ) ) {
   if ($data[$key]) {
    $strings .= $key . " = '" . SQLString($val) . "', ";
   }
  }
  $strings=ereg_replace(", $", " ", $strings);
  return $strings;
 }

 Function dbQuery ($statement) {
  global $dbconfig;
#  echo "<p><code>dbQuery: $statement</code></p>";
  $db=pg_Connect($dbconfig["sqlserver"],$dbconfig["sqlport"],"","",$dbconfig["defaultdb"]);
  if (! $db) {
    PrintError("Could not connect to database");
    return 0;
  }
  $result=pg_Exec($db,$statement);
  if(!$result)
    PrintError(pg_ErrorMessage($db));
  pg_Close($db);
  return $result;
 }

 Function dbResult ($result,$i,$field) {
  return pg_Result($result,$i,$field);
 }

 Function dbNumRows ($result) {
  return pg_NumRows($result);
 }

 Function dbResultArray ($result,$i) {
  if (!$result)
	return "";
  return pg_Fetch_Array($result,$i);
 }

?>
