Forum » Advanced Programming » Database class (Begin)
What else?
Berichten: 1180
avatar
Online Stuur privebericht
Het is een begin van mijn database class en natuurlijk even net als we zijn begonnen door de andere OO'ers hier laten raten:

Code | Selecteer Alles
minimaliseren
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
<?php

/**
 * @author Patrick rennings
 * @copyright 2010
 */

Class Database {
    
    
    protected 
$_connect;
    protected static 
$_connection_type;
    
    
    public function 
__construct $hostname$username$password null$database$ctype )
    {
        
        if ( !
is_int $ctype ) )
        {
            throw new 
exception ' Connection type is not an integer. (Line 17) ' );    
        }
        elseif ( 
$ctype != OR $ctype != )
        {
            throw new 
exception ' Connection type is not correct. (Line 22) ' );
        }
        elseif ( !
is_string $hostname ) OR !is_string $username ) OR !is_string($datbase) )
        {
            throw new 
exception ' Connection parameters arnt integers (Line 26) ' );
        }
        else
        {
            
self::$_connection_type $ctype;
            
            if ( 
self::$_connection_type == )
            {
                
$this->_connect mysql_connect$hostname$username$password );
                if ( ! 
$this->_connect )
                {
                    throw new 
exception ' SQL connection could not be established. (Line 36) ' );
                }
                
                if ( ! 
mysql_select_db($database$this->_connect) )
                {
                    throw new 
exception ' Database connection could not be established. (Line 42) ' );
                }
            }
            if ( 
self::$_connection_type == )
            {
                
$this->_connect = new mysqli $hostname$username$password$database );
                if ( ! 
$this->_connect )
                {
                    throw new 
exception ' SQL connection could not be established. (Line 36) ' );
                } 
            } 
        }
    }
    
    public function 
SqlQuery $QryLine )
    {
        if ( 
self::$_connection_type == 1)
        {
            
$DbQuery mysql_query ($this->_connect$Qryline );
            if ( ! 
$DbQuery )
            {
                throw new 
exception ' Mysql error accured ' mysql_error ( ) );
            }
            else
            {
                return 
$DbQuery;
            }
        }
        if ( 
self::$_connection_type == 2)
        {
            if ( ! 
$DbQuery $this->_connect->query$QryLine ) )
            {
                throw new 
exception ' Mysqli error accured ' $this->_connnect->error );
            }
            else
            {
                return 
$DbQuery;
            }
        }
    }
}

?>

05-04-2010 10:13
Dit topic is 106 keer bekeken door 6 verschillende leden
Reacties op: "Database class (Begin)"
1
Webdeveloper & Programmeu
Berichten: 1001
avatar
Offline Stuur privébericht
Maak of verschillende functions waar je mysqli en mysql in gebruikt of gebruik verschillende databases.
Je maakt een hoofd class waar je de connection aanmaakt.
En in de andere classes ga je het resultaat en de functions maken voor mysql en voor mysqli.

Het is dan overzichtelijker en logischer.
05-04-2010 11:17
Reageer op: "Database class (Begin)"
1
Je kan niet reageren omdat je niet bent ingelogd. Inloggen of Aanmelden