Scripts

"Smart" mysql

Dit script voert een mysql query uit, haalt alle records op en geeft een array met de records terug. Om de verbinding met de server op te starten hoef je niets te doen, dit gebeurt automatisch. Om een query uit te voeren met de standaarddatabase gebruik je $mysql->mysql( "select * from test"); om zelf een database op te geven doe je $mysql->mysql("select * from test" , 'mijndatabase' ); Aan het einde van je script wordt de verbinding vanzelf verbroken. Hoe ziet het resultaat eruit? $db = $mysql->mysql(..) retourneert een array die er bijvoorbeeld zo uitziet: $db[ 1 ][ 'veldnummer1' ] $db[ 1 ][ 'veldnummer2' ] $db[ 2 ][ 'veldnummer1' ] $db[ 2 ][ 'veldnummer2' ] $db[ 0 ] bevat wat informatie over de query, zoals de uitvoertijd [ 'time' ], aantal results [ 'number' ], en in geval v.e. insert query de ingevoegde auto_increment id [ 'id' ], zie script voor meer stats. $mysql->total_time bevat de totale tijd die is gebruikt voor alle mysql queries samen, $mysql->total_queries bevat het totaal aantal uitgevoerde queries. $mysql->time bevat de tijd die de laatste query kostte. De functie $mysql->gather_stats( ) geeft diverse statistieken terug, die in het geval van $config[ 'auto_retrieve_result' ] = true en $config[ 'auto_gather_stats' ] = true al opgenomen zijn in $db[ 0 ], zie hierboven. Verder heb ik diverse configuratieopties toegevoegd, zie script voor meer uitleg. Veel succes!

smart-mysql
[code]
<?php

$config = array( );
$config[ 'mysql_use_persistent' ] = false;			//Gebruik een persistentie connectie (true) of niet (false)
$config[ 'mysql_server' ] = 'localhost';			//De server waar mysql op draait (meestal 'localhost')
$config[ 'mysql_user' ] = 'root';					//De mysql gebruikersnaam
$config[ 'mysql_password' ] = '';					//Je mysql wachtwoord
$config[ 'mysql_standard_database' ] = 'databasestandaard';	//Als je zelf geen database opgeeft gebruik dan deze
$config[ 'mysql_use_ansi_mode' ] = true;			//Veiliger MYSQL'en in ANSI mode
$config[ 'mysql_display_errors' ] = true;			//Toon foutmeldingen van mysql zelf
$config[ 'auto_retrieve_result' ] = true;			//Automatisch resultaat ophalen en returnen
$config[ 'auto_mysql_free_result' ] = true;			//Leegt automatisch het resultaat wanneer je het resultaat automatisch op laat halen
$config[ 'auto_gather_stats' ] = true;				//Verzamel automatisch statistieken, zoals de query tijd en aantal opgehaalde resultaten wanneer het resultaat automatisch wordt opgehaald
define( 'ERROR_CONNECTION_FAILED' , 'Het maken van een verbinding met de mysql server is mislukt.' . ( $config[ 'mysql_display_errors' ] == true ) ? ' Mysql retourneerde: ' : '' );
define( 'ERROR_SELECTDB_FAILED' , 'Het selecteren van de juiste database mislukt.' . ( $config[ 'mysql_display_errors' ] == true ) ? ' Mysql retourneerde: ' : '' );
define( 'ERROR_DOQUERY_FAILED' , 'Het uitvoeren van de mysql query mislukt.' . ( $config[ 'mysql_display_errors' ] == true ) ? ' Mysql retourneerde: ' : '' );

class mysqlconnector{
	
	private $connection_identifier;
	private $active_database;
	private $query_result;
	public $time;
	private $query;
	public $total_queries;
	public $total_time;
	
	public function __construct( )
	{
		GLOBAL $config;
		if( $config[ 'mysql_use_persistent' ] == true )
		{
			if( ! $this->connection_identifier = mysql_pconnect( $config[ 'mysql_server' ] , $config[ 'mysql_user' ] , $config[ 'mysql_password' ] ) )
			{
				trigger_error( ERROR_CONNECTION_FAILED . ( $config[ 'mysql_display_errors' ] == true ) ? mysql_errno( ) . ': ' . mysql_error( ) : '' );
			}
		}
		else
		{
			if( ! $this->connection_identifier = mysql_connect( $config[ 'mysql_server' ] , $config[ 'mysql_user' ] , $config[ 'mysql_password' ] ) )
			{
				trigger_error( ERROR_CONNECTION_FAILED . ( $config[ 'mysql_mysql_errors' ] == true ) ? mysql_errno( ) . ': ' . mysql_error( ) : '' );
			}
		}
	}
	
	public function mysql( $query , $database = '' )
	{
		GLOBAL $config;
		$this->total_queries += 1;
		$this->time = microtime( );
		if( $database == '' )
		{
			$database = $config[ 'mysql_standard_database' ];
		}
		if( $config[ 'mysql_use_ansi_mode' ] == true )
		{
			mysql_query( "SET SESSION sql_mode = 'ANSI,ONLY_FULL_GROUP_BY'" , $this->connection_identifier );
		}
		if( $this->select_database( $database ) )
		{
    		return $this->execute_query( $query );
    	}
    	else
    	{
    		return false;
    	}
	}
	
	private function select_database( $database )
	{
		GLOBAL $config;
		if( $database != $this->active_database )
		{
			if( ! mysql_select_db( $database , $this->connection_identifier ) )
			{
				trigger_error( ERROR_SELECTDB_FAILED . ( $config[ 'mysql_display_errors' ] == true ) ? mysql_errno( ) . ': ' . mysql_error( ) : '' );
				return false;
			}
			else
			{
				return true;
			}
		}
		return true;
	}
	
	private function execute_query( $query )
	{
		GLOBAL $config;
		$this->query = $query;
		if( $this->query_result = mysql_query( $query , $this->connection_identifier ) )
		{
			$this->total_time += microtime( ) - $this->time;
			$this->time = microtime( ) - $this->time;
			if( $config[ 'auto_retrieve_result' ] == true )
			{
				return $this->retrieve_result( );
			}
			else
			{
				return $this->query_result;
			}
		}
		else
		{
			trigger_error( ERROR_DOQUERY_FAILED . ( $config[ 'mysql_display_errors' ] == true ) ? mysql_errno( ) . ': ' . mysql_error( ) : '' );
			return false;
		}
	}
	
	private function retrieve_result( )
	{
		GLOBAL $config;
		if( substr( strtoupper( $this->query ) , 0 , 6 ) == "SELECT" || substr( strtoupper( $this->query ) , 0 , 4 ) == "SHOW" ||
			substr( strtoupper( $this->query ) , 0 , 7 ) == "EXPLAIN" || substr( strtoupper( $this->query ) , 0 , 8 ) == "DESCRIBE" )
		{
    		$db_result = array( );
    		$db_result[ 0 ] = ( $config[ 'auto_gather_stats' ] == true ) ? $this->gather_stats( ) : '';
    		$db_num_fields = @mysql_num_fields( $this->query_result );
    		$i = 1;
    		while( $content = mysql_fetch_array( $this->query_result , MYSQL_ASSOC ) )
    		{
    			for( $j = 0; $j < $db_num_fields; $j++ )
    			{
    				$db_result[ $i ][ mysql_field_name( $this->query_result , $j ) ] = $content[ mysql_field_name( $this->query_result , $j ) ];
    			}
    			$i++;
    		}
    		if( $config[ 'auto_mysql_free_result' ] == true )
    		{
    			@mysql_free_result( $this->query_result );
    		}
    		return $db_result;
    	}
    	return '';
	}
	
	public function gather_stats( )
	{
		$stats = array( );
		if( substr( strtoupper( $this->query ) , 0 , 5 ) == "SELECT" || substr( strtoupper( $this->query ) , 0 , 3 ) == "SHOW" ||
			substr( strtoupper( $this->query ) , 0 , 6 ) == "EXPLAIN" || substr( strtoupper( $this->query ) , 0 , 7 ) == "DESCRIBE" )
		{
			$stats[ 'number' ] = @mysql_num_rows( $this->query_result );
		}
		else
		{
			 $stats[ 'number' ] = @mysql_affected_rows( $this->connection_identifier );
		}
		$stats[ 'time' ] = $this->time;
		$stats[ 'server' ] = mysql_stat( );
		$stats[ 'id' ] = ( substr( strtoupper( $this->query ) , 0 , 1 ) == "I" ) ? mysql_insert_id( ) : 0;
		$stats[ 'info' ] = @mysql_info( );
		return $stats;
	}
	
	public function __destruct( )
	{
		mysql_close( $this->connection_identifier );
	}
}
$mysql = new mysqlconnector( );
?>
[/code]

Reacties

0
Nog geen reacties.