<?
	#---------------------------------------------------------------------#
	# This source code is (c)2000 White Pajamas, Inc                      #
	# see http://www.stonekeep.com/license.html for licensing information #
	#---------------------------------------------------------------------#

	#--------------------------------------------------------------------
	# $Id: mysql.php3,v 1.22 2000/12/14 19:13:52 shevett Exp $
	#----------------------------------------------------------------------------
	# PHP Database independent for PTS supporting: mysql
	#----------------------------------------------------------------------------

	function db_connect($target) {
		global $dbhost,$dbname,$dbuser,$dbpasswd,$server,$port,$options,$tty,$conn;
		if (! mysql_pconnect($dbhost,$dbuser,$dbpasswd)) {
			report_error("db_connect",mysql_errno(),mysql_error());
		}
	}

	function db_query($qstring) {
		global $dbhost,$dbname,$server,$conn;
		$result = mysql($dbname,"$qstring");
		LogIt("DEBUG","db_query","$qstring");
		if (mysql_errno()) {
			$foostring=addslashes($qstring);
			LogIt("ERR","db_query","$qstring");
			report_error("db_query",mysql_errno(),mysql_error(),"Query was: $foostring");
		}
		return $result;
	};

	function db_numrows($qhandle) {
		global $dbhost,$dbname,$server;
		$result = mysql_numrows($qhandle);
		if (mysql_errno()) {
			report_error("db_numrows",mysql_errno(),mysql_error());
		}
		return $result;
	};

	function db_result($qhandle,$row,$field) {
		global $dbhost,$dbname,$server;
		if ($qhandle == 0) {
			report_error("db_result",mysql_errno(),"Attempt to fetch from empty handle.");
		} else {
			$result = mysql_result($qhandle,$row,$field);
			if (mysql_errno()) {
				report_error("db_result",mysql_errno(),mysql_error());
			}
		}
		return $result;
	};

	function db_listfields($tname) {
		global $dbhost,$dbname,$server,$port,$options,$tty,$conn,$last_table;
		$result = mysql_Listfields($dbname,$tname);
		if (mysql_errno()) {
			report_error("db_listfields",mysql_errno(),mysql_error());
		}
		return $result;
	};

	function db_numfields($lhandle) {
		global $dbhost,$dbname,$server;
		$result = mysql_numfields($lhandle);
		if (mysql_errno()) {
			report_error("db_numfields",mysql_errno(),mysql_error());
		}
		return $result;
	};


	function db_fieldname($lhandle,$fnumber) {
		global $dbhost,$dbname,$server;
		$result = mysql_fieldname($lhandle,$fnumber);
		if (mysql_errno()) {
			report_error("db_fieldname",mysql_errno(),mysql_error());
		}
		return $result;
	};

	function db_fieldtype($lhandle,$which) {
		global $dbhost,$dbname,$server;
		$result = mysql_fieldtype($lhandle,$which);
		if (mysql_errno()) {
			report_error("db_fieldtype",mysql_errno(),mysql_error());
		}
		return $result;
	};

	function db_fieldlen($lhandle,$which) {
		global $dbhost,$dbname,$server,$port,$options,$tty,$conn,$last_table;
		$result=mysql_fieldlen($lhandle,$which);
		if (mysql_errno()) {
			report_error("db_fieldlen",mysql_errno(),mysql_error());
		}
		return $result;
	};

	function db_affected_rows($qhandle) {
		$result = mysql_affected_rows();
		if (mysql_errno()) {
			report_error("db_affected_rows",mysql_errno(),mysql_error());
		}
		return $result;
	};
	
	function db_fetch_array($qhandle) {
		if ($qhandle == 0) {
			report_error("db_fetch_array",mysql_errno(),"Attempt to fetch from empty handle.");
		} else {
			$result = mysql_fetch_array($qhandle);
			if (mysql_errno()) {
				report_error("db_fetch_array",mysql_errno(),mysql_error());
			}
		}
		return $result;
	};
	
	function db_seek($qhandle,$qrows) {
		$result = mysql_data_seek($qhandle,$qrows);
		if (mysql_errno()) {
			report_error("db_seek",mysql_errno(),mysql_error());
		}
		return $result;
	};
	
	function db_fieldflags($qhandle,$which) {
		$result = mysql_fieldflags($qhandle,$which);
		if (mysql_errno()) {
			report_error("db_fieldflags",mysql_errno(),mysql_error());
		}
		return $result;
	};
	
	function db_primarykey($tablename) {
		$fn	=	db_listfields("$tablename");
		$n 	=	db_numfields($fn);
		$priinfo[1]="";
		$counter=0;
		while ($counter < $n) {
			$finfo=db_fieldflags($fn,$counter);
			LogIt("DEBUG","db_primarykey","finfo for $$counter is $finfo");
			if (ereg("primary",$finfo)) {
				$priinfo[0] = db_fieldname($fn,$counter);
				$ftype=db_fieldtype($fn,$counter);
				if ("$ftype" == "string") {
					$priinfo[1]="'";
				}
			}
			$counter++;
		}
		return ($priinfo);
	}

       function db_insertid($r) {
		$result = mysql_insert_id();
		if (mysql_errno()) {
			report_error("db_insertid",mysql_errno(),mysql_error());
		}
		return $result;
	}

	function db_errno() {
		$result = mysql_errno();
		if (mysql_errno()) {
			report_error("db_errno",mysql_errno(),mysql_error());
		}
		return $result;
	}

	function db_error() {
		$result = mysql_error();
		if (mysql_errno()) {
			report_error("db_error",mysql_errno(),mysql_error());
		}
		return $result;
	}


?>
