PDO TUTORIAL SERIES 3
To continue our pdo tutorial, we will improve on our database class... if this is your first read, you can catch up:
1. Tutorial 1
2. Tutorial 2
3. Side Tutorial: Preparation for Tutorial 3
Ok, now you can continue.
I made the following changes to our database class:
1. I Created a constructor
(Further reading: http://php.net/manual/en/oop4.constructor.php,
http://php.net/manual/en/language.oop5.decon.php )
2. I modified our database connect method, i named it open_connection(), so we can have another method, close_connection().
3. I Created close_connection method
4. Instantiate the database class
My reasons:
1. So that each time i instantiate a new object of the database class (with the key word new), the open_connection method will be automatically called.
2. Just appropriate naming convention, so that our code can be easily understood by another developer and to avoid future confusion for yourself.
3. This method is for closing the database connection, although, leaving the database connection open has not been known to cause serious damage, but its good practice to close connection.
4. This way, we do not need to instantiate it again when we require the class. (keep reading you'll understand this)
Now, my modified index.php file
to test it, i made a little addition to my open_connection(), to tell me that database was successfully connected:
And here's what we have:
Now, to look closely at our close_connection method.
With PDO, it is not absolutely necessary to close connection, like i have said earlier... but i still prefer to do it. So, to close connection, you set it to null.
My index page again:
Our result:
NB: Method and functions are one and the same in PHP don't get confused.
In the next tutorial (next week), we'll improve on the database class and start taking note of some security features.
I hope this tutorial has been useful so far, drop a comment, question, correction, suggestion etc
Don't forget to share.
Good news for all, for the first time i'm making my code available... click here to download (ZIP)
1. Tutorial 1
2. Tutorial 2
3. Side Tutorial: Preparation for Tutorial 3
Ok, now you can continue.
I made the following changes to our database class:
(Further reading: http://php.net/manual/en/oop4.constructor.php,
http://php.net/manual/en/language.oop5.decon.php )
2. I modified our database connect method, i named it open_connection(), so we can have another method, close_connection().
3. I Created close_connection method
4. Instantiate the database class
My reasons:
1. So that each time i instantiate a new object of the database class (with the key word new), the open_connection method will be automatically called.
2. Just appropriate naming convention, so that our code can be easily understood by another developer and to avoid future confusion for yourself.
3. This method is for closing the database connection, although, leaving the database connection open has not been known to cause serious damage, but its good practice to close connection.
4. This way, we do not need to instantiate it again when we require the class. (keep reading you'll understand this)
Now, my modified index.php file
to test it, i made a little addition to my open_connection(), to tell me that database was successfully connected:
And here's what we have:
Now, to look closely at our close_connection method.
With PDO, it is not absolutely necessary to close connection, like i have said earlier... but i still prefer to do it. So, to close connection, you set it to null.
My index page again:
Our result:
NB: Method and functions are one and the same in PHP don't get confused.
In the next tutorial (next week), we'll improve on the database class and start taking note of some security features.
I hope this tutorial has been useful so far, drop a comment, question, correction, suggestion etc
Don't forget to share.
Good news for all, for the first time i'm making my code available... click here to download (ZIP)
Post a Comment