[PHP] MySQL static class

Gestart doorEpichotel9 reacties21 weergaven

10 berichten · pagina 1 van 1

Epichotel

Lid · Topicstarter

Berichten
69
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Hi,

Hierbij deel ik mijn 2e gemaakte class, een MySQL static class.
Hiermee kan je een verbinding opzetten, en alle algemene MySQL functies. Ik wil het graag in OOP, maar omdat ik een beginner ben met OOP, weet ik niet precies wat ik nog meer OOP kan schrijven in de class.

[COLOR= #0000BB]<?php[/COLOR]
[COLOR= #FF8000]/**
 * @ Product:        MySQL static class
 * @ Author:        Joey Blankendaal            
 * @ Website:        JoeyBlankendaal.com
 * @ Date:            10 May 2012
 * @ Class name:    MySQL
 * @ Copyright:        (C) 2010 - 2012 Joey Blankendaal - All rights reserved
 *
 * Redistributions of source code must retain the above copyright
 * notice and this (list of) condition(s).
 */[/COLOR]
 
[URL="http://www.php.net/class"]class[/URL] MySQL
[COLOR= #007700]{[/COLOR] 
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #0000BB]$connect_error[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #0000BB]$select_db_error[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #0000BB]$query_error[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #0000BB]$connection[/COLOR]         [COLOR= #007700]=[/COLOR] null[COLOR= #007700];[/COLOR]    
    
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] Connect[COLOR= #007700]([/COLOR][COLOR= #0000BB]$dbServer[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000][/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$dbUsername[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000][/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$dbPassword[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000][/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$dbDatabase[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000][/COLOR][COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/if"]if[/URL] [COLOR= #007700](!@[/COLOR][URL="http://www.php.net/mysql_connect"]mysql_connect[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$dbServer[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$dbUsername[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$dbPassword[/COLOR][COLOR= #007700]))
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]connect_error [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]Kon niet verbinden met de database, de geselecteerde database bestaat niet.[/COLOR][COLOR= #007700];[/COLOR]
            
            [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]connect_error[COLOR= #007700];
        }[/COLOR]
        [URL="http://www.php.net/elseif"]elseif[/URL] [COLOR= #007700](!@[/COLOR][URL="http://www.php.net/mysql_select_db"]mysql_select_db[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$dbDatabase[/COLOR][COLOR= #007700]))
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]select_db_error [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]Kon niet verbinden met de database, de server, username of password klopt niet.[/COLOR][COLOR= #007700];[/COLOR]
            
            [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]select_db_error[COLOR= #007700];
        }
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] Query[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700])
    {[/COLOR]
        [COLOR= #0000BB]$query[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/mysql_query"]mysql_query[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700],[/COLOR] self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$connection[/COLOR][COLOR= #007700]);[/COLOR]
        [URL="http://www.php.net/if"]if[/URL] [COLOR= #007700](![/COLOR][URL="http://www.php.net/mysql_error"]mysql_error[/URL][COLOR= #007700]())
        {[/COLOR]
            [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$query[/COLOR][COLOR= #007700];
        }[/COLOR]
        [URL="http://www.php.net/else"]else[/URL]
        [COLOR= #007700]{[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]query_error [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]Kon de query niet uitvoeren, dit is de error:[/COLOR] [COLOR= #007700].[/COLOR] [URL="http://www.php.net/mysql_error"]mysql_error[/URL][COLOR= #007700]();[/COLOR]
            
            [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]query_error[COLOR= #007700];
        }
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] NumRows[COLOR= #007700]([/COLOR][COLOR= #0000BB]$resource[/COLOR][COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/mysql_num_rows"]mysql_num_rows[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$resource[/COLOR][COLOR= #007700]);
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] Fetch[COLOR= #007700]([/COLOR][COLOR= #0000BB]$resource[/COLOR][COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/mysql_fetch_assoc"]mysql_fetch_assoc[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$resource[/COLOR][COLOR= #007700]);
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] FetchObject[COLOR= #007700]([/COLOR][COLOR= #0000BB]$resource[/COLOR][COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/mysql_fetch_object"]mysql_fetch_object[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$resource[/COLOR][COLOR= #007700]);
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] FetchArray[COLOR= #007700]([/COLOR][COLOR= #0000BB]$resource[/COLOR][COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/mysql_fetch_array"]mysql_fetch_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$resource[/COLOR][COLOR= #007700]);
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] Escape[COLOR= #007700]([/COLOR][COLOR= #0000BB]$string[/COLOR][COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/mysql_real_escape_string"]mysql_real_escape_string[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$string[/COLOR][COLOR= #007700]);
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] Info[COLOR= #007700]()
    {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/mysql_info"]mysql_info[/URL][COLOR= #007700]([/COLOR]self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$connection[/COLOR][COLOR= #007700]);
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] Close[COLOR= #007700]()
    {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/mysql_close"]mysql_close[/URL][COLOR= #007700]([/COLOR]self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$connection[/COLOR][COLOR= #007700]);
    }
}[/COLOR]
?>


Tips zijn welkom!
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Wat heeft dit voor voordeel t.o.v. de standaardfuncties?
0
Berichten
5.897
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Rik schreef:
Wat heeft dit voor voordeel t.o.v. de standaardfuncties?
Dingen zien er korter uit. + standaard foutenafhandeling als ik het zo zie biggrin
0
Epichotel

Lid · Topicstarter

Berichten
69
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Rik schreef:
Wat heeft dit voor voordeel t.o.v. de standaardfuncties?
Niet veel, alleen een connectie maken is handiger en kortere functies. Maar het is puur om classes te leren schrijven en OOP daarin te gebruiken.
0
Berichten
10.421
Reputatie
4
Punten
0,00
Lid sinds
Laatst gezien
En waarom gebruik je geen exceptions voor je foutmeldingen?
0
Berichten
8.602
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Ik dacht dat Joey Blankendaal Joeyl op o2xs was?
0
Berichten
8.649
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Misschien ies die het totaal aantal uitgevoerde queries optelt, en de lengte dat het duurde om die allemaal uit te voeren te berekenen zodat je dat onderaan je site kan zetten.
0
Berichten
5.897
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Milan schreef:
Ik dacht dat Joey Blankendaal Joeyl op o2xs was?
Dit is een andere Joey, vind het ook verwarrend confused
0
Epichotel

Lid · Topicstarter

Berichten
69
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Milan schreef:
Ik dacht dat Joey Blankendaal Joeyl op o2xs was?
Niet meer, ik ben van account geruild met mijn broertje omdat hij perse een hoge actiefheid wou.
Gamedealer schreef:
Volgens mij heb je een deel hier uit gehaald:
http://www.basmilius.com/downloads/script?file=class.mysql.php
Nee, alleen die functions zoals Escape, Fetch etc., die zitten in elke standaard MySQL class.
Aarclay schreef:
En waarom gebruik je geen exceptions voor je foutmeldingen?
Kende ik nog niet, ga ik doen!
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren