Location: PHPKode > scripts > sqltoolkit > sqltoolkit/sql.php
<?php
/*

    sql.php - PHP functions for obscuring datasource, database_result and database_connection classes
    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("_SQL_TOOLKIT_FUNCTIONS_")){
	define("_SQL_TOOLKIT_FUNCTIONS_", TRUE);
	


/*

	CONFIGURATION OPTIONS 
	
*/



// Set this to your include path for the system. This is most important on 
// shared hosting solutions where you may not be able to put these files
// into a global include directory.
$includesPath = "";


// optional files, you can comment out the rows you know you will not use
// to save processing time.
require($includesPath . "sql_xml.php");	// for importing xml data (not required for export)


/*

	FILE DEPENDENCY

*/




// these are the three class types required
require($includesPath . "datasource.php");
require($includesPath . "database_connection.php");
require($includesPath . "database_result.php");

// location of your datasource config file. You will need to edit this file to
// setup all the datasources you will want to use.
require($includesPath . "datasource_config.php");


/* 

	DATA MANIPULATION AND RETURN FUNCTIONS FOR CLASS: database_connection
	
*/





// class:database_connection database_connect(class:datasource);
function database_connect(&$connection_settings){
	return new database_connection($connection_settings);
}
function database_query(&$db_connection, $query){
	return new database_result($db_connection->query_connection($query), $db_connection->get_dbtype());
}
function database_close(&$db_connection){
	return $db_connection->close_connection();
}





/* 

	DATA MANIPULATION AND RETURN FUNCTIONS FOR CLASS: database_result
	
*/





// utility functions
function database_fetch_data(&$db_result){
	return $db_result->result_data();
}
function database_fetch_info(&$db_result){
	return $db_result->result_info();
}
function database_free_result(&$db_result){
	// this is a hack since as of php 4.3.1 unsetting a pass by reference doesn't really work.
	// unset($db_result);
	$db_result = 0;
	return TRUE;
}


// return result rows
function database_fetch_assoc(&$db_result){
	return $db_result->fetch_assoc();
}
function database_fetch_row(&$db_result){
	return $db_result->fetch_row();
}
function database_fetch_object(&$db_result){
	return $db_result->fetch_object();
}


// return info about result set
function database_num_rows(&$db_result){
	return $db_result->num_rows();
}
function database_num_fields(&$db_result){
	return $db_result->num_fields();
}


// manipulate the internal pointer
function database_data_seek(&$db_result, $row){
	return $db_result->data_seek($row);
}





/*

	HTML DISPLAY FUNCTIONS

*/





// this function allows a quick dump of the data returned in an HTML table
function database_result_dump($dataset){
	$dataset->data_seek(0);
	print "
	<style>
	.database_result_dump_heading{font-family: verdana, arial, helvetica; font-size: 8pt; color: black; background-color: cccccc; }
	.database_result_dump_data{font-family: verdana, arial, helvetica; font-size: 8pt; color: black; background-color: ffffaa; }
	</style>
	";
	print "<table border=\"1\" cellpadding=\"1\" cellspacing=\"0\">\n";
	if ($dataset->num_rows() * $dataset->num_fields()){
		print "<tr>\n";
		print "<th class=\"database_result_dump_heading\" colspan=\"" . ($dataset->num_fields() + 1) . "\">database record: " . $dataset->num_rows() . " rows returned</td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<td class=\"database_result_dump_heading\"></td>\n";
		$columns = array_keys($dataset->fetch_assoc());
		foreach ($columns AS $column){
			if ($column != "database_result_info") {
				print "<td class=\"database_result_dump_heading\">" . $column . "</td>";
			}
		}
		print "</tr>\n";	
		$dataset->data_seek(0);
		for ($row=0; $row<$dataset->num_rows(); $row++){
			$this_row_assoc = $dataset->fetch_assoc();
			print "<tr>\n";
			print "<td class=\"database_result_dump_heading\" align=\"center\">" . $row . "</td>\n";
			foreach ($columns AS $column){
				if ($column != "database_result_info") {
					print "<td class=\"database_result_dump_data\">" . $this_row_assoc[$column] . "</td>\n";
				}
			}		
			print "</tr>\n";
		}
	} else {
		print "<tr>\n";
		print "<th class=\"database_result_dump_heading\">database result: no data returned.</td>\n";
		print "</tr>";
	}
	print "</table>\n";
	return true;
} // end function database_result_dump($result)





/*

	CONVIENIENCE FUNCTIONS 

*/






// database_result database_quick_query(string, string);
function database_quick_query($datasource, $query){
	$dbConnection 		= new database_connection($datasource);
	if ($dbConnection == FALSE){
		die ("<b>sql.php</b>: database_quick_query() invalid datasource connection, aborting query.<br />\n");
	}	
	$myDataset    		= $dbConnection->query_connection($query);
	$normalizedData     = new database_result($myDataset, $datasource->get_type());
	$disconectstatus    = $dbConnection->close_connection();
	return $normalizedData;
}


// convert the database_result to standard xml syntax
function database_result_xml($db_result, $encoding = "ISO-8859-1"){
	$xml_string = "<?xml version=\"1.0\" encoding=\"". $encoding . "\"?>\n";
	database_data_seek($db_result,0);
	// print "<!DOCTYPE project-listing SYSTEM \"http://freshmeat.net/backend/fm-projects-0.1.dtd\">\n";
	$xml_string .= "<dataset cols=\"" . database_num_fields($db_result) . "\" rows=\"" . database_num_rows($db_result) . "\">\n";
	while($row = database_fetch_assoc($db_result)){
		$xml_string .= "  <row>\n";
		$cols = array_keys($row);
		foreach ($cols as $column){
			$xml_string .= "    <" . $column . ">" . htmlentities($row[$column]) .  "</" . $column . ">\n";
		}
		$xml_string .= "  </row>\n";
	}
	$xml_string .= "</dataset>\n";
	return $xml_string;
}



} // end functions code lock

?>
Return current item: sqltoolkit