Hoi,
Dit is mijn eerste tut en hierin ga ik, voor diegene die het nog niet kan, uitleggen hoe je een simpele MySQL Static class kan maken.
Om te beginnen gaan we de class aanmaken, we noemen hem maar MySQL.
Hierna voegen we een variable toe waar de verbinding in blijft zitten.. Dit doe je door een private iets te maken in de class.. De class gaat er dan zo uit zien:
Nu kunnen we voor zover wel beginnen met functies maken..
Eerst maar eens uitleggen hoe je een functie binnen de class maakt.
Terug naar de MySQL class..
Let op, in de code staat alleen de functie die je moet toevoegen aan je class
Laten we maar beginnen met de Init functie om de verbinding te maken.
Bij de init functie moet je je database gegevens meegeven.. Dus je gebruikersnaam, ww, host en db naam.
Deze functie is eigenlijk de belangrijkste functie uit de hele class, want zonder een verbinding kan je geen mysql gegevens ophalen hea..
Okee, nu de Init functie is gemaakt kunnen we nu verder met de andere functies, zoals Query.
Nu kun je al Queries uitvoeren.. maar hoe fetch je ze nu? Dat laat ik hieronder zien.
Da was simpel hea?
Nu heb je eigenlijk al de belangrijkste functies..
Maar mysql_num_rows is ook wel handig..
Nahh nu heb je toch echt wel alles wat nodig is..
Uitbreidingen komen wel op het vervolg.. waar ik nu geen zin aan heb
Gebruik:
Hope you like it
- Bas
Dit is mijn eerste tut en hierin ga ik, voor diegene die het nog niet kan, uitleggen hoe je een simpele MySQL Static class kan maken.
Om te beginnen gaan we de class aanmaken, we noemen hem maar MySQL.
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/Class"]Class[/URL] MySQL[COLOR= #007700]{
}[/COLOR]
?> Hierna voegen we een variable toe waar de verbinding in blijft zitten.. Dit doe je door een private iets te maken in de class.. De class gaat er dan zo uit zien:
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/Class"]Class[/URL] MySQL[COLOR= #007700]{[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #0000BB]$verbinding[/COLOR][COLOR= #007700];
}[/COLOR]
?> Nu kunnen we voor zover wel beginnen met functies maken..
Eerst maar eens uitleggen hoe je een functie binnen de class maakt.
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/Class"]Class[/URL] Bas[COLOR= #007700]{[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] Is[COLOR= #007700](){[/COLOR]
[URL="http://www.php.net/return"]return[/URL] [COLOR= #DD0000]"Uber"[/COLOR][COLOR= #007700];
}
}[/COLOR]
[COLOR= #FF8000]//Oproepen van de functie[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] Bas[COLOR= #007700]::[/COLOR]Is[COLOR= #007700]();[/COLOR] [COLOR= #FF8000]//Outpunt: Uber[/COLOR]
?> Terug naar de MySQL class..
Let op, in de code staat alleen de functie die je moet toevoegen aan je class
Laten we maar beginnen met de Init functie om de verbinding te maken.
Bij de init functie moet je je database gegevens meegeven.. Dus je gebruikersnaam, ww, host en db naam.
Deze functie is eigenlijk de belangrijkste functie uit de hele class, want zonder een verbinding kan je geen mysql gegevens ophalen hea..

[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] Init[COLOR= #007700]([/COLOR][URL="http://www.php.net/array"]array[/URL] [COLOR= #0000BB]$data[/COLOR][COLOR= #007700]){[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR]self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$verbinding[/COLOR] [COLOR= #007700]= @[/COLOR][URL="http://www.php.net/mysql_connect"]mysql_connect[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$data[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]host[/COLOR][COLOR= #007700]],[/COLOR][COLOR= #0000BB]$data[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]gebruiker[/COLOR][COLOR= #007700]],[/COLOR][COLOR= #0000BB]$data[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]ww[/COLOR][COLOR= #007700]],[/COLOR][COLOR= #0000BB]$data[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]db[/COLOR][COLOR= #007700]])){[/COLOR]
[COLOR= #FF8000]//Er is iets mis gegaan tijdens het verbinden..[/COLOR]
[URL="http://www.php.net/die"]die[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/mysql_error"]mysql_error[/URL][COLOR= #007700]());
}[/COLOR] [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
[COLOR= #FF8000]//Er is verbinding! Hier kunnen eventuele kleine build-ins worden gemaakt..[/COLOR]
[COLOR= #007700]}
}[/COLOR]
?> Okee, nu de Init functie is gemaakt kunnen we nu verder met de andere functies, zoals Query.
[COLOR= #0000BB]<?php[/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]$str[/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]$str[/COLOR][COLOR= #007700],[/COLOR] self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$verbinding[/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]
[COLOR= #FF8000]//Er is iets mis gegaan in de query![/COLOR]
[URL="http://www.php.net/die"]die[/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]
?> Nu kun je al Queries uitvoeren.. maar hoe fetch je ze nu? Dat laat ik hieronder zien.
[COLOR= #0000BB]<?php[/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]
?> Da was simpel hea?
Nu heb je eigenlijk al de belangrijkste functies..Maar mysql_num_rows is ook wel handig..
[COLOR= #0000BB]<?php[/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]
?> Nahh nu heb je toch echt wel alles wat nodig is..
Uitbreidingen komen wel op het vervolg.. waar ik nu geen zin aan heb

Gebruik:
[COLOR= #0000BB]<?php
$data[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/Array"]Array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]host[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #DD0000]localhost[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]gebruiker[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #DD0000]Gebruiker[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]ww[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #DD0000]Wachtwoord[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]db[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #DD0000]Database naam[/COLOR][COLOR= #007700]);[/COLOR]
MySQL[COLOR= #007700]::[/COLOR]Init[COLOR= #007700]([/COLOR][COLOR= #0000BB]$data[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$query[/COLOR] [COLOR= #007700]=[/COLOR] MySQL[COLOR= #007700]::[/COLOR]Query[COLOR= #007700]([/COLOR][COLOR= #DD0000]"SELECT username FROM users"[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL][COLOR= #DD0000]Er zijn [/COLOR] [COLOR= #007700].[/COLOR] MySQL[COLOR= #007700]::[/COLOR]NumRows[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700]) .[/COLOR] [COLOR= #DD0000] gebruikers gevonden![/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/while"]while[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$fetch[/COLOR] [COLOR= #007700]=[/COLOR] MySQL[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR]Fetch[COLOR= #007700]([/COLOR][COLOR= #0000BB]$query[/COLOR][COLOR= #007700])){[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$fetch[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]username[/COLOR][COLOR= #007700]] .[/COLOR] [COLOR= #DD0000]<br/>[/COLOR][COLOR= #007700];
}[/COLOR]
?> Hope you like it

- Bas


?
unctie();