Location: PHPKode > projects > PLiMa > plima-0.5.2/inc/db-share.inc.php
<?
/***
	*   pLiMa - php List Manager
  *   Copyright (C) 2003  Jinn Koriech (hide@address.com)
	*
  *		This file is part of pLiMa.
	*
	*   pLiMa is free software; you can redistribute it and/or modify
	*   it under the terms of the GNU General Public License as published by
	*   the Free Software Foundation; either version 2 of the License, or
	*   any later version.
	*
	*   pLiMa is distributed in the hope that it will be useful,
	*   but WITHOUT ANY WARRANTY; without even the implied warranty of
	*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	*   GNU General Public License for more details.
	*
	*   You should have received a copy of the GNU General Public License
	*   along with pLiMa; if not, visit http://www.gnu.org or write to the 
	*   Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
	*	  Boston, MA  02111-1307  USA
	*	
	*/

/*****************************************************************************
 *                                                                           *
 * Usage:                                                                    *
 *   $_GLOBAL['conn'] = db_connect();                                        *
 *   $result = db_query($sql);                                               *
 *   $data = db_fetch_array($result, $row = 0);                              *
 *   $int = db_num_rows($result);                                            *
 *   $int = db_insert_id($table=0, $field=0);                                *
 *   $int = db_affected_rows($result);                                       *
 *   db_close($_GLOBAL['conn']);                                             *
 *                                                                           *
 *****************************************************************************/

//define("DB_TYPE", "1"); // PostgreSQL
define("DB_TYPE", "2"); // MySQL

function db_connect() {
				if ( DB_TYPE == 1 ) {
								$_GLOBAL['conn']	= pg_connect("dbname=".DB_NAME." host=".DB_HOST."	user=".DB_USER." password=".DB_PASS." port=".DB_PORT) 
									or $error = pg_errormessage();;
				} elseif ( DB_TYPE == '2' ) {
								$_GLOBAL['conn'] = mysql_pconnect(DB_HOST, DB_USER, DB_PASS) 
									or $error = mysql_error();;
								mysql_select_db(DB_NAME, $_GLOBAL['conn']);
				}

				//check the connection
				if (!$_GLOBAL['conn'])	{
								print	 "Database connection error.<BR><B>The back-end said:</B> $error<BR>";
								return;
				}
				return $_GLOBAL['conn'];
}

function db_query($sql) {
	if ( !$_GLOBAL['conn'] ) {
		$_GLOBAL['conn'] = db_connect();
	}

	// cleanup SQL for PHP versions! (sic)
	if (substr($sql,strlen($sql)-1,1)==";") {
		$sql=substr($sql,0,strlen($sql)-1);
	}

	if (DB_TYPE==1) {
		$result = @pg_exec($_GLOBAL['conn'], $sql) or $err[] = pg_errormessage();
		if ( count($err) > 0 ) {
			$err[] .= "[SQL] " . $sql;
		for ( $idx = 0; $idx < count($err); $idx++ ) {
			print "error $idx : " . $err[$idx] . "<BR>";
		}
		unset($err);
		}
	} elseif (DB_TYPE==2) {
		$result = mysql_query($sql, $_GLOBAL['conn']);
	}
	return $result;
}

function db_fetch_array($result, $row = 0) {
	if (DB_TYPE==1) {
		$entry = @pg_fetch_array($result, $row);
	} elseif (DB_TYPE==2) {
		$seek = @mysql_data_seek($result, $row); // or die ("Failed to go to row $row<BR>$sql<BR>" . mysql_error() ) ;
		$entry = @mysql_fetch_array($result);
	}
	return $entry;
}

function db_num_rows($result) {
	global $debug;
	if (DB_TYPE==1) {
		$numRows = pg_numRows($result);
	} elseif (DB_TYPE==2) {
		$numRows = mysql_num_rows($result) or $error = mysql_error();
		if ( $debug ) echo $error."<BR>";
	}
	return $numRows;
}

function db_insert_id($table=0, $field=0) {
	if (DB_TYPE==1) {
		$seq = $table . "_" . $field . "_seq";
		$sql = "SELECT currval('$seq');";
		$result = db_query($sql);
		$result = db_fetch_array($result, 0);
		$oid = $result[0];
	} elseif (DB_TYPE==2) {
		$oid = mysql_insert_id($_GLOBAL['conn']);
	}
	return $oid;
}

function db_affected_rows($result) {
	if (DB_TYPE==1) {
		$affected = ($result == 0) ? 0 : 1;
	} elseif (DB_TYPE==2) {
		$affected = mysql_affected_rows($_GLOBAL['conn']);
	}
	return $affected;
}

function db_close($conn) {
	if (DB_TYPE==1) {
		pg_close($conn);
	} elseif (DB_TYPE==2) {
		mysql_close($conn);
	}
}
?>
Return current item: PLiMa