Location: PHPKode > scripts > TXTDB to MySQL > txtdb-to-mysql/txtdb2mysql.class.php
<?php

/////////////// TXTDB2MySQL CLASS //////////////////////////////////////////////////////////
// This class uses the same function names and veriables as the TXTDB flat file SQL emulator. 
// It is good for anyone already using or considering using the original TXTDB as it helps
// paint you OUT of the corner if you decide to use MySQL without drastically modifying
// your code. Also, the manual for TXTDB can be used with this class.
//
// A real-world example of this class can using both TXTDB and MySQL can be found at:
// http://www.openportals.com/en2/index.php?sv=&category=Web%20Design~Download
/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////
// Query result class
////////////////////////////////
class SQL_Result{

	var $resulthandle;
	var $record;
	var $recpointer;
	var $database;
	var $dbConnectionID;


	function SQL_Result($resulthandle="", $database="", $dbConnectionID=""){
		$this->resulthandle = $resulthandle;
		$this->database = $database;
		$this->dbConnectionID = $dbConnectionID;
		$this->recpointer = 0;
	}


	//////////////////////////////////////////////////////////////////////
	// Navigates to te next row (so it will be the current row)
	// Sets the Position of the ResultSet Object to the next Record (row)
	// Return value: 
	//	If there was a next Record: TRUE 
	//	If the end of the ResultSet is reached: FALSE
	//////////////////////////////////////////////////////////////////////
	function next(){

		$this->record = @mysql_fetch_array($this->resulthandle);

		if($this->record == ""){ return false; }
		else{ 
			$this->recpointer ++;
			return true; 
		}

	}///////////////


	////////////////////////////////////////////////////////////////////////
	// Navigates to te previous row (so it will be the current row)
	// Sets the Position of the ResultSet Object to the previous Record (row).
	//Return value:
	//   If there was a previous Record: TRUE
	//   If the start of the ResultSet is reached: FALSE
	////////////////////////////////////////////////////////////////////////
	function Prev() {

		if($this->recpointer < 1) return false;

		$this->recpointer --;

		if($this->recpointer <  0) $this->recpointer = 0;


		if(mysql_data_seek($this->resulthandle, $this->recpointer)){

			$this->record = @mysql_fetch_array($this->resulthandle);
			return true;

		}else{
			return false;
		}


	}/////////////////


	///////////////////////////////////////////////////////////////////
	// Set's the Position of the Resultset to 1 before the first Record
	///////////////////////////////////////////////////////////////////
	function reset() {

		if(mysql_num_rows($this->resulthandle) == 0){

			$this->recpointer = 0; 
			return false;

		}else	if(mysql_data_seek($this->resulthandle, 0)){
			$this->recpointer = 0; 
			return true;
		}else{
			return false;
		}

	}/////


	////////////////////////////////////////////////////////
	// Returns the current Position (row) of the ResultSet
	////////////////////////////////////////////////////////
	function getPos() {

		return $this->recpointer; 

	}/////

	//////////////////////////////////////////////////////
	// Sets the current Position of the ResultSet Object.
	//////////////////////////////////////////////////////
	function SetPos($newpos) {

		if($newpos > -1 && $newpos < $this->getRowCount()){
			if(mysql_data_seek($this->resulthandle, $newpos)){
				$this->recpointer = $newpos; 
			}
		}

	}/////


	///////////////////////////////////////////
	// Returns the row-count of the Result Set.
	//////////////////////////////////////////
	function getRowCount(){

		return @mysql_num_rows($this->resulthandle);

	}//////


	///////////////////////////////////////////////////////////////////////////////////
	// Returns the number of columns the ResultSet has (= the number of fields per row)
	///////////////////////////////////////////////////////////////////////////////////
	function getRowSize(){

		$numcolumns = @mysql_num_fields($this->resulthandle); 

		return $numcolumns;

	}//////


	///////////////////////////////////////////////////////////////////
	// Returns a array containing the column-names of the ResultSet. 
	// This values can be used, for example, in
	// getCurrentValueByName() or getValueByName() calls.
	///////////////////////////////////////////////////////////////////
	function getColumnNames(){

		$this_table = mysql_field_table($this->resulthandle,0);

		$fields = mysql_list_fields($this->database, $this_table, $this->dbConnectionID);

		$columns = mysql_num_fields($fields);
	
		$column_names = array();

		for ($i = 0; $i < $columns; $i++) {
			$column_names[] = mysql_field_name($fields, $i);
		}

		return $column_names;



	}/////////////////



	///////////////////////////////////////////////////////////////////////////////////
	// Returns the Value of the Column colName of the current Row.
	// The current Row can be changed with the functions next(), prev() oder setPos().
	///////////////////////////////////////////////////////////////////////////////////
	function getCurrentValueByName($field){

		return($this->record[$field]);

	}////////////


	///////////////////////////////////////////////////////////////////////
	// Returns the Value of the Column colNr at the current Position (row).
	// The current Position (Record, row) can be changed with the functions
	// next(), prev() or setPos().
	///////////////////////////////////////////////////////////////////////
	function getCurrentValueByNr($field){

		return($this->record[$field]);

	}////////////


	//////////////////////////////////////////////////////////
	// Returns the Value of the Coumn colNome of the Row rowNr
	//////////////////////////////////////////////////////////
	function getValueByName ($rowNr, $colName){

		if($rowNr < mysql_num_rows($this->resulthandle)){

			$this->SetPos($rowNr);

			$returnval = @mysql_fetch_array($this->resulthandle);

			return $returnval[$colName];
		
		}else{
			return false;
		}

	}//////




	//////////////////////////////////////////////////////////////////////////
	// Returns the Value of the Column colNr of the Row rowNr.
	//////////////////////////////////////////////////////////////////////////
	function getValueByNr ($rowNr, $colNr){

		if($rowNr < mysql_num_rows($this->resulthandle)){

			$this->SetPos($rowNr);

			$returnval = @mysql_fetch_array($this->resulthandle);

			return $returnval[$colNr];

		}else{
			return false;
		}

	}////////////



	////////////////////////////////////////////////////////////////////////////
	// Returns the Values of the current Row as array. The values in the Array 
	// have the same order as in the SQL Query you supply.
	// The current Row (position) can be changed with the functions next(), prev() or setPos().
	////////////////////////////////////////////////////////////////////////////
	function getCurrentValues(){

		return($this->record);

	}//////


	//////////////////////////////////////////////////////////////////////////////////////
	// Returns the Values of the row rowNr as array. The values in the array have the same 
	// Order as in the SQL Statement. (and if you use *, the same order as in the table)
	//////////////////////////////////////////////////////////////////////////////////////
	function getValues($rowNr){

		if($rowNr < mysql_num_rows($this->resulthandle)){

			$this->SetPos($rowNr);

			return @mysql_fetch_array($this->resulthandle);

		}else{
			return false;
		}

	}//////


}

//////////////////////////////////////////
// Parent class
//////////////////////////////////////////
class MySQL_database{
  
  var $dbConnectionID;
  var $queryID;
  var $record; 
  var $host;
  var $database;
  var $user;
  var $password;
 

	function MySQL_database($host="localhost", $db="openportal", $user="root", $pwd=""){

		$this->host = $host;
		$this->database = $db;
		$this->user = $user;
		$this->password = $pwd;
		$this->connect();
	}

	function connect(){

		$this->dbConnectionID = @mysql_pconnect($this->host, $this->user, $this->password);

	
		if(!$this->dbConnectionID){
			echo(mysql_errno().":".mysql_error());
			exit;
		}else{
			$status = @mysql_select_db($this->database, $this->dbConnectionID);
			if(!$status){
				echo(mysql_errno().":".mysql_error());
				exit;
			}
		} 
	}

  
	function executeQuery($sql){

		if(empty($this->dbConnectionID)) $this->connect();

		$this->queryID = @mysql_query($sql, $this->dbConnectionID);
    
		if(!$this->queryID){
			echo(mysql_errno().":".mysql_error());
			return false;

		}else{
			$this_result = new SQL_Result($this->queryID,$this->database, $this->dbConnectionID);
			return $this_result;
		}
	}



	//////////////////////////
	function db_Table_Exists($tablename){
	
		$tables = @mysql_query("SHOW TABLES", $this->dbConnectionID);
		
		while (list($this_tablename)=mysql_fetch_array($tables)) {
			if($this_tablename == $tablename) {

				return true;
			}
		}

		return false;

	}

}//////////////

?>
Return current item: TXTDB to MySQL