$field; } /* Check if we are connected to the database. Used for simple connection checking later on */ function isConnected( ) { if( $this->mysql_connection_link && $this->mysql_db_connection_link ) return true; else return false; } /* Return the result of the mysql_connect function call or die and return the error */ function getConnectLink( ) { return ( mysql_connect( $this->server, $this->mysql_user, $this->mysql_pass ) or die( "MySql Connect Error: " . mysql_error( ) ) ); } /* Same as above except executed on the database */ function getDbConnectLink( ) { return ( mysql_select_db( $this->mysql_database ) or die( "MySql Select Db Error: " . mysql_error( ) ) ); } /* Executes a MySQL query. Can also be used simply for an INSERT query. */ function executeQuery( $query ) { $result = mysql_query( $query ); $err = mysql_error( ); if( $err != "" ) return $err; return $result; } /* Dynamically retrieves all the column names from a table and then pushes the results of the query (SHOW COLUMNS FROM $table ) onto the mysql_columns array */ function getTableColumns( $table ) { $query = $this->executeQuery( "SHOW COLUMNS FROM $table" ); while( $obj = mysql_fetch_object( $query ) ) { array_push( $this->mysql_columns, $obj->Field ); } } /* Counts the columns in the table */ function countColumnsInTable( $table ) { $query = $this->executeQuery( "SHOW COLUMNS FROM $table" ); return mysql_num_rows( $query ); } /* Get a table name at a specific index in the mysql_columns array */ function getTableColumnAtIdx( $table, $idx ) { //Fill it if its empty. Checking if it is empty so it is only done on the first //time rather than every single call to the function if( empty( $this->mysql_columns ) ) $this->getTableColumns( $table ); return $this->mysql_columns[$idx]; } /* Checks if a column exists. This is quite buggy and needs fixing */ function checkIfColumnsExist( $table, $columns_from_table, $columns_from_query ) { for( $i = 0; $i < $this->countColumnsInTable( $table ); $i++ ) { for( $j = 0; $j < sizeof( $columns_from_query ); $j++ ) { if( $columns_from_query[ $i ] == "*" ) return true; if( $columns_from_table[ $i ] == $columns_from_query[ $j ] ) return true; else continue; } } return false; } /* Parses the column names from a SELECT query */ function parseColumnNamesFromQuery( $query ) { $query = strtolower( $query ); if( ( sizeof( $this->mysql_parsed_columns ) > 0 ) || ( sizeof( $this->mysql_row_results ) > 0 ) ) { $size = sizeof( $this->mysql_parsed_columns ); for( $i = 0; $i < $size; $i++ ) array_pop( $this->mysql_parsed_columns ); $size = sizeof( $this->mysql_row_results ); for( $i = 0; $i < $size; $i++ ) array_pop( $this->mysql_row_results ); } $fmt = explode( "from", $query ); $select = $fmt[0]; $select = preg_replace('/\s/', '',$select ); $select = preg_replace( '/select/', '', $select ); $select_fmt = explode( ",", $select ); $count = count( $select_fmt ); if( $select_fmt[0] == "*" ) { $debug = new cDebug( ); $debug->enableDebugging( true ); $debug->debugBuffer( "Wild cards are currently unsupported (TODO: Implement this feature )", 3 ); } for( $i = 0; $i < $count; $i++ ) { array_push( $this->mysql_parsed_columns, $select_fmt[ $i ] ); } } /* Executes the SELECT query, parses the names, and stores the results of the SELECT query in the mysql_row_results array */ function executeSelectQuery( $query ) { $res = $this->executeQuery( $query ); $this->parseColumnNamesFromQuery( $query ); while( $row = mysql_fetch_assoc( $res ) ) { for( $i = 0; $i < sizeof( $this->mysql_parsed_columns ); $i++ ) { array_push( $this->mysql_row_results, $row[ $this->mysql_parsed_columns[ $i ] ] ); } } } /* Returns the result set stored in a specific index number in the mysql_row_result array. This will only retrieve information of the last query executed with executeSelectQuery() */ function getSelectResult( $idx ) { return $this->mysql_row_results[ $idx ]; } /* Returns the size of the mysql_row_results array which in turn also returns the size of the result set of the previous SELECT query executed with executeSelectQuery() */ function getSelectResultSize( ) { return sizeof( $this->mysql_row_results ); } /* Constructor for setting up the mysql connection */ function __construct( $user, $pass, $server, $database ) { $this->mysql_user = $user; $this->mysql_pass = $pass; $this->mysql_server = $server; $this->mysql_database = $database; $this->mysql_connection_link = $this->getConnectLink( ); $this->mysql_db_connection_link = $this->getDbConnectLink( ); } } ?>