Location: PHPKode > scripts > sqltoolkit > sqltoolkit/database_connection.php
<?php
/*
    database_connection.php - A PHP class for managing a database nutral connection
    Copyright (C) 2003 Erik Giberti (AF-Design), All rights reserved.

    This program 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
    (at your option) any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

if (!defined("_DATABASE_CONNECTION_CLASS_")){
	define("_DATABASE_CONNECTION_CLASS_",TRUE);

	// bool database_connection(array);
	class database_connection{
		var $connection; 		  // to hold the open connection
		var $connection_settings; // to hold the open connections settings
		/*
			CONSTRUCTOR
		*/
		function database_connection($datasource_settings){
			// resource open_connection(class:datasource);
			if (!defined("_DATABASE_CONNECTION_CLASS_FUNCTIONS_")){
				define("_DATABASE_CONNECTION_CLASS_FUNCTIONS_",TRUE);
				function open_connection($ds){
					switch(strtoupper( $ds->get_type() )){
						case "MYSQL":
							$dbConnection = @mysql_connect($ds->get_server(), $ds->get_username(), $ds->get_password())
								or die("<b>sql.php</b>: database_connect() was unable to open database connection to the specified datasource \"" . mysql_error() . "\"");
							@mysql_select_db($ds->get_database(),$dbConnection)
								or die("<b>sql.php</b>: database_connect() was unable to select the database \"" . mysql_error() . "\"");
							break;
						case "MSSQL":
							$dbConnection = @mssql_connect($cs->get_server(), $cs->get_username(), $cs->get_password())
								or die("<b>sql.php</b>: database_connect() was unable to open database connection to the specified datasource \"" . mssql_get_last_message() . "\"");
							@mssql_select_db($ds->get_database(), $dbConnection)
								or die("<b>sql.php</b>: database_connect() was unable to select the database \"" . mssql_get_last_message() . "\"");
							break;
						case "POSTGRES":
						case "POSTGRES95":
						case "POSTGRESQL":
							$connectionString = "";
							if ($ds->get_server()   != '') { $connectionString .= "host="     . $ds->get_server() . " "; }
							if ($ds->get_port()     != '') { $connectionString .= "port="     . $ds->get_port() . " "; }
							if ($ds->get_database() != '') { $connectionString .= "dbname="   . $ds->get_database() . " "; }
							if ($ds->get_username() != '') { $connectionString .= "user="     . $ds->get_username() . " "; }
							if ($ds->get_password() != '') { $connectionString .= "password=" . $ds->get_password() . " "; }
							$dbConnection = pg_connect($connectionString)
								or die("<b>sql.php</b>: database_connect() was unable to open database connection to the specified datasource \"" . "\"");
							break;
						case "MSQL":
							$dbConnection = @msql_connect($ds->get_server(), $ds->get_username(), $ds->get_password())
								or die("<b>sql.php</b>: database_connect() was unable to open database connection to the specified datasource \"" . msql_error() . "\"");
							@msql_select_db($ds->get_database(),$dbConnection)
								or die("<b>sql.php</b>: database_connect() was unable to select the database \"" . msql_error() . "\"");
							break;
						case "ODBC":
							$dbConnection = @odbc_connect($ds->get_server(), $ds->get_username(), $ds->get_password())
								or die("<b></b>: database_connect() was unable to connect to the database \"" . odbc_error() . "\"");
							$result = @odbc_autocommit($dbConnection, TRUE);
							break;
						default:
							$dbConnection = FALSE;
							break;
					}
					return $dbConnection;
				} // end function open_connection()
			} // end code lock
			$this->connection_settings = $datasource_settings;
			$this->connection = open_connection($datasource_settings);
			
		}
	
		// resource query_connection(string)
		function query_connection($query){
			switch(strtoupper($this->connection_settings->get_type())){
				case "MYSQL":
					$myDataset = @mysql_query($query, $this->connection) 
						or die("<b>sql.php</b>: database_query_connection() was unable to execute the query you requested \"" . mysql_error() . "\"");
					break;
				case "MSSQL":
					$myDataset = @mssql_query($query, $this->connection)
						or die("<b>sql.php</b>: database_query_connection() was unable to execute the query you requested \"" . mssql_get_last_message() . "\"");
					break;
				case "POSTGRES":
				case "POSTGRES95":
				case "POSTGRESQL":
					$myDataset = @pg_query($this->connection, $query)
						or die("<b>sql.php</b>: database_query_connection() was unable to execute the query you requested \"" . "\"");
					break;
				case "MSQL":
					$myDataset = @msql_query($query, $this->connection) 
						or die("<b>sql.php</b>: database_query_connection() was unable to execute the query you requested \"" . msql_error() . "\"");
					break;
				case "ODBC":
					$myDataset = @odbc_exec($this->connection, $query)
						or die("<b>sql.php</b>: database_query_connection() was unable to execute the query you requested \"" . odbc_error() . "\"");
					break;
				default:
					$myDataset = FALSE;
					break;
			}
			return $myDataset;
		} // end function query_connection()
		
		// bool close_connection(void);
		function close_connection(){
			switch(strtoupper($this->connection_settings->get_type())){
				case "MYSQL":
					$result = @mysql_close($this->connection);
					break;
				case "MSSQL":
					$result = @mssql_close($this->connection);
					break;
				case "POSTGRES":
				case "POSTGRES95":
				case "POSTGRESQL":
					$result = @pg_close($this->connection);
					break;
				case "MSQL":
					$result = @msql_close($this->connection);
					break;
				case "ODBC":
					$result = @odbc_close($this->connection);
					break;
				default:
					$result = FALSE;
					break;
			}
			return $result;
		} // end function close_connection()

		function get_dbtype(){
			return $this->connection_settings->get_type();
		}

	} // end class database_connection

} // end defined("_DATABASE_CONNECTION_CLASS_")

?>
Return current item: sqltoolkit