PHP PDO Wrapper Class | MikeM. - SimpleBB 11-14-2014, 03:56 AM
#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:
or
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.
![[Image: AzzXnJil.png]](http://i.imgur.com/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 byThis 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.
![[Image: KujcCmCl.png]](http://i.imgur.com/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.
![[Image: wjFt5Wll.png]](http://i.imgur.com/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
line 46-47: Else we just execute the query and just do exactly the same as line 44
This method is called for everything that does not return a result set, such as but not limited to: DELETE, UPDATE, INSERT
![[Image: PMnTJIel.png]](http://i.imgur.com/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
Enjoy and ofcourse give proper credits if you use it, no one likes leechers!
Yours sincerely,
Mike M.
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"
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]](http://i.imgur.com/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
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]](http://i.imgur.com/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
Spoiler: Running a Non-Query (true/false output query)
![[Image: wjFt5Wll.png]](http://i.imgur.com/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

line 46-47: Else we just execute the query and just do exactly the same as line 44

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)
Spoiler: End Class, Use the Class, Close the PHP tag
![[Image: PMnTJIel.png]](http://i.imgur.com/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
Spoiler: Running a query
Spoiler: Running a query with preparement variables
Data:
![[Image: nPr6vpnl.png]](http://i.imgur.com/nPr6vpnl.png)
Usage:
![[Image: theozRvl.png]](http://i.imgur.com/theozRvl.png)
Output:
![[Image: Swoodpxl.png]](http://i.imgur.com/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"
![[Image: nPr6vpnl.png]](http://i.imgur.com/nPr6vpnl.png)
Usage:
![[Image: theozRvl.png]](http://i.imgur.com/theozRvl.png)
Output:
![[Image: Swoodpxl.png]](http://i.imgur.com/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.
![[Image: 76PfAYUl.png]](http://i.imgur.com/76PfAYUl.png)
![[Image: 5yGLPvcl.png]](http://i.imgur.com/5yGLPvcl.png)
![[Image: hqshjFEl.png]](http://i.imgur.com/hqshjFEl.png)
![[Image: boiudFul.png]](http://i.imgur.com/boiudFul.png)
![[Image: NCCWyn4l.png]](http://i.imgur.com/NCCWyn4l.png)
![[Image: oTZviHvl.png]](http://i.imgur.com/oTZviHvl.png)
![[Image: XvdhH19l.png]](http://i.imgur.com/XvdhH19l.png)
![[Image: e1MGmMIl.png]](http://i.imgur.com/e1MGmMIl.png)
![[Image: tC0WErql.png]](http://i.imgur.com/tC0WErql.png)
![[Image: IVsyiPCl.png]](http://i.imgur.com/IVsyiPCl.png)
![[Image: 8ZzCxzjl.png]](http://i.imgur.com/8ZzCxzjl.png)
![[Image: xgh1FVDl.png]](http://i.imgur.com/xgh1FVDl.png)
![[Image: FqGEckdl.png]](http://i.imgur.com/FqGEckdl.png)
![[Image: eTwayzCs.jpg]](http://i.imgur.com/eTwayzCs.jpg)
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)


![[Image: 4j6lf4yi8c.png]](https://pod.so/i/4j6lf4yi8c.png)