smart-mysql

Gesponsorde koppelingen

PHP script bestanden

  1. smart-mysql

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?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( );
?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.