Login Register


Tutorial PHP PDO Wrapper Class | MikeM. - SimpleBB filter_list
Author
Message
PHP PDO Wrapper Class | MikeM. - SimpleBB #1
Hi there,
Thanks for coming by.
I made some PDO wrapper class for my own software and I wanted to give that out already as I already have posted it on StackOverflow (SO) 9th of Movember (Keep your mustache and beard for the whole month November to support the fight against Prostate Cancer!)

Well I will take it apart step by step explaining you what the arguments are and how to use it and how it works...

This class is made by ME from scretch so don't bitch about it that it isn't mine, I had some support from the PHP chatroom in SO with 2 lines for the prepared statements part.

We use the word method instead of function if we talk about programming language functions.
We only use the word function if we are talking about a method which calculates something such as: Class Math function devide
The function devide will be called a function, if it didn;t had anything to do with calculating something/using anything math/algebra related we would call it a method.

The name of this class would be: pdo_class.php

To use this class you need to write:
PHP Code:
require("/path/to/class/pdo_class.php"
or
PHP Code:
include("/path/to/class/pdo_class.php"

Make sure you also require or include the config file where the config array is located in! this must be done BEFORE requiring/including the pdo_class!

We would suggest using the require for this as we do not need the pdo_class to be loaded in the file you are using, we only want to read items out of the methods called.


Well enough chit chat xD let's go!

I will use PICTURES to make sure you aren't just copy pasting everything.
Spoiler: Start PHP, Message, Creating Class & Constructor
[Image: AzzXnJil.png]

Line 1: is obvious this is the start of a php script.
Line 2-7: is the copyright/powered by part, you can skip this ofcourse or you can leave it in your script for the credits, I'd appreciate it if you take it aswell in your script.
Line 8: We are going to define/create a new class called: Database - remember a class MUST start with a capital, this is also called INITCAP
Line 9: We are going to declare a new variable for the class Database which will only be used in that class and cannot be called for the outside! [this kiddo got jailed in the Database class]
Line 11: This is the start of the constructor you can see that by
PHP Code:
__construct
This constructor takes the argument $config which is an array.
Line 12-16: Creating new variables using the $config array values.
Line 18: Once we call the class we also want to make sure the database connection is open to use.


Spoiler: Connect to the Database
[Image: KujcCmCl.png]

Line 21: We are starting with the word "private" which means we are only allowed to use this method in this class the method is a function so we write out that it is a function with the name connect which has the arguments of the variables we set from the $config array in the constructor
Line 23: We set the class variable "$db" to a new PDO class/connection with the arguments of the local variables but now with some regular string values
Line 24: We set a PHP PDO attribute for the emulation of the prepared statements, we turned this to false so incorrect queries such as "INSERT;" may return false at the preparing time and will not be passed further.
Line 25: We set a PHP PDO attribute for the error mode
Line 26: We catch the PDO Exception
Line 27-31: Is a custom error page which shows the information from these variables.


Spoiler: Close the connection
[Image: 76PfAYUl.png]

I don't think it's worth to be explained... it just sets the class variable "$db" to a null value, which actually means "I AM CLOSED, FIND ANOTHER PARTYHOUSE!"


Spoiler: Running a Non-Query (true/false output query)
[Image: wjFt5Wll.png]

Line 39: Creating the method nonQuery with the query arguments but here also comes in a bit of magic, which you will see from line 41!
Line 40: We create a new local variable "$run" (which says "whoosh" outside of this method) which will "prepare" the query and basicly says "I am done please give me the cookies!"
Line 41: We check for each argument passed into method after the $query. I made a little mistake there because it have to be > 1 because now it always goes to that part of the php script, this doesn't mean it doesn't work...
Line 42: Get the arguments out of the method
Line 43: I basicly don't remember why I put this into here.... I think to remove the query.... well I am quite sure of removing the query from the array but héhé I am a night coder so shhh xD
Line 44: Return the boolean of the execution with the arguments of the method, if the execute passed return true, else it would return false and we have a sad pirate Sad
line 46-47: Else we just execute the query and just do exactly the same as line 44 Smile

This method is called for everything that does not return a result set, such as but not limited to: DELETE, UPDATE, INSERT


Spoiler: Running a query (result set output query)
[Image: 5yGLPvcl.png]

Same story as by a nonQuery but on this end there will be a result set returning and not a true/false

IGNORE THE COMMENT ON LINE 69, IT WAS AN EXPERIMENT AND NOT FOR THIS CLASS MAINLY AS IT IS NOW!


Spoiler: End Class, Use the Class, Close the PHP tag
[Image: PMnTJIel.png]

Line 78: Close the class
Line 79: Declare and Instance the variable "$db" with the class Database
Line 80-85: is the copyright/powered by part, you can skip this ofcourse or you can leave it in your script for the credits, I'd appreciate it if you take it aswell in your script.
Line 86: Close the PHP script with the Ending PHP Tag


Spoiler: Setting the Config Array
[Image: hqshjFEl.png]

This how our setup of the config array looks like.
Use the same kind of config values else you'd need to change the naming in the class itselves!


Spoiler: Running a nonQuery
Data:
[Image: boiudFul.png]

Usage:
[Image: NCCWyn4l.png]

Output:
[Image: oTZviHvl.png]

Data After:
[Image: XvdhH19l.png]


Spoiler: Running a query
Data:
[Image: nPr6vpnl.png]

Spoiler: Method 1
Usage:
[Image: e1MGmMIl.png]

Output:
[Image: tC0WErql.png]


Spoiler: Method 2
Usage:
[Image: IVsyiPCl.png]

Output:
[Image: 8ZzCxzjl.png]


Spoiler: Method 3 - using method 2 to take out a column defined
Usage:
[Image: xgh1FVDl.png]

Output:
[Image: FqGEckdl.png]



Spoiler: Running a query with preparement variables
Data:
[Image: nPr6vpnl.png]

Usage:
[Image: theozRvl.png]

Output:
[Image: Swoodpxl.png]

Using the prepared statement with variables is just a matter of adding a question mark in the query at the point where you want to get an argument to be added
so query($query, arg1, arg2) expects:
Query: SELECT * FROM table WHERE id=? OR id=?
and the arguments needs to be filled out as how it should be outputted so
ARG1 will be filled out on the question mark BEFORE the "OR" and ARG2 will be filled out on the question mark AFTER the "OR"



Enjoy and ofcourse give proper credits if you use it, no one likes leechers!

Yours sincerely,
Mike M.
(12-01-2014, 06:44 AM)dopamine Wrote: I think I got trolled hard by the script kiddies on this forum and they do actually understand that I am their boss
[Image: eTwayzCs.jpg]

Reply

RE: PHP PDO Wrapper Class | MikeM. - SimpleBB #2
I just checked out your site simplebb.net
First thing I noticed:
[Image: 4j6lf4yi8c.png]
what in the FUCK is an advertention

also what the fuck is up with the design, I now have ebola


User was warned for this post (LQP/offtopic)-Rei
wow-Xeru

Reply

RE: PHP PDO Wrapper Class | MikeM. - SimpleBB #3
(11-18-2014, 09:56 PM)Xeru Wrote: I just checked out your site simplebb.net
First thing I noticed:
[Image: 4j6lf4yi8c.png]
what in the FUCK is an advertention

also what the fuck is up with the design, I now have ebola


User was warned for this post (LQP/offtopic)-Rei
wow-Xeru

The reason you have been warned for the post in the quote is because it is offtopic.
This thread is not about SimpleBB it's about the wrapper class, and I think and pretty sure can assume that this thread clearly has nothing to do about simplebb and only about the wrapper class
(12-01-2014, 06:44 AM)dopamine Wrote: I think I got trolled hard by the script kiddies on this forum and they do actually understand that I am their boss
[Image: eTwayzCs.jpg]

Reply

RE: PHP PDO Wrapper Class | MikeM. - SimpleBB #4
Have you tried working with Brackets? its such a good web editor Biggrin

Reply







Users browsing this thread: 1 Guest(s)