Header Ads

Seo Services

SIDE TUTORIAL: PHP OOP - SECURE AND RE-USABLE DATABASE CONNECTION

In preparation for our next PDO tutorial, i have decided to share this important piece with you...
I hope you find it useful.


BETTER WAY OF PERFORMING DATABASE CONNECTION

Personally, i prefer creating re-usable codes, i don't know of other developers out there.. but i'd rather not retype codes.. and so, while performing database connection i do it in a php file using CONSTANTS. That way i can change just the CONSTANTS, export database and all will work well for any project...



To illustrate this, we are starting with project for Company A with the following details:

Company Full Name: Company A
Company Abbreviated Name: C.A
Company Phone Contact: 110293458
Company Address: 123 Company A close

Host: localhost
Database Name: db_comp_A
Username: (will use root for this project)
password:  (no password)

If i also have another company (Company B), with very different details... but similar requirements (which is usually the case)...

Company Full Name: Company B
Company Abbreviated Name: C.B
Company Phone Contact: 454565454
Company Address: 677 Area, Company  B close

Database Name: db_comp_B
Username: (will use root for this project)
password:  (no password)

I write my code in such a way that i can easily change certain info and all will be well. Keep reading to understand what i'm talking about.


Here's my folder structure:



This way, the admin and public end of my application use the same library.

Here's my constants.php (located in tutorial/library/constants.php)


I use the 'defined' function/method to check if the constant has already been defined, in which case it will return null, and if the constant has not been defined, it will be defined. If you are not familiar with this shorthand if statement (called Ternary Operator or Logic) you can do more reading on it.

Start here:
php.net/manual/en/control-structures.elseif.php
php.net/manual/en/control-structures.alternative-syntax.php
davidwalsh

After declaring the constants, i create a database class, database.class.php (located in tutorial/library/database.class.php). I use this class naming convention to help me easily identity classes in my application.



In my database class i perform all my database operations, but for now, i'm just going to transfer all our connection code from tutorial 2.


From what you can see, i created a file functions.php where all my functions will be. I included all required documents in my database class, i created a method, connnect (php uses function to represent functions and methods used in some other languages).

In the method connect, i performed my database connection.

Now to apply our constants


And that's it... we're done with this tutorial... but lets try it out.

Create index.php in your public folder, and do the following:


We firstly require all needed documents, then we instantiate an object of the database class, we then call the method (connect) of the database class using the arrow.

When you run this, you should run into different types of error. I suggest you run it and try to debug on your own before you continue reading...

The first error should be the required files in our database class. We navigated to library/constants.php and library/functions.php instead of just requiring the files without including folder name... i believe you know why this is so, and if you don't its because the database.class.php, functions.php and constants.php are in the same folder - library, and so no need to include the folder name.

Like so:


The second error, if you have not corrected it, should give you our customized error message..

And that is because our database name is not correct or we have not created the database. Just go to the constant file, change the database name or create the database and try connecting again... You should get a blank page meaning your connection was successful... But if you're still having problems with this... drop a comment and i'll help you out...

The third bug should be the log folder, your error is probably not being logged into the log folder as it should, thats because

1. We have not created the log folder
2. We need to adjust our log_error function to navigate to the folder.



I do hope this tutorial has been helpful... I'll continue with our PDO tutorial this week..


Keep reading and don't forget to share.



No comments