Soulfire in our code we've been using my Escuela lie to connect to and interact with the database and
we've been using procedural as opposed to object oriented code.
However my Eskew ally Ah so has an object oriented interface.
So each procedural function has an object oriented equivalent
and it's okay to use either but not a good idea to mix them as your code will be difficult to read.
We could convert all our code to use the object oriented version of my Eskew ally but if we're going
to start using object oriented code to interact with the database P HP offers an alternative method
with P HP data objects are pedigo we can access the database in much the same way we've been doing it
using my s qaly.
However PDA does have several advantages over my Eskew ally.
First the show isn't just limited to my esky Well armoury dbe you can connect to all of these database
management systems using exactly the same pdaf code.
So once you know pto you can work with pretty much any relational database management system without
having to learn more code.
In addition to this working with prepared statements is easier with pto as you can have named place
holders instead of just question marks.
Plus I find the pdl code needed to fetch data using a prepared statement is easier to use than that
of my escudo LIE.
Having said that my escudo is still a good choice.
If you know you're going to stick exclusively to my esky Well my Escuela does offer a very slight performance
advantage over pdl but unless your site is very large and has millions of hits it's not going to make
any difference.
And I think the benefits of using pdl outweigh any disadvantages for this reason.
I prefer to use pdl Whenever I need to work with a relational database p s p.
So now that we know how to use classes and objects let's start using pdl in our project to connect to
the database.
First let's create a class that represents the database we're connecting to the database at the moment
with this function and we're using it for various different scripts.
Let's create a class that represents the database instead that we can use from any script that needs
it.
Let's create a new folder to put our class files in called classes in here.
Will create a new file called databased up p p.
And note that the file name starts with a capital letter
in here will create a new class called database.
And again note that the class name matches the file name.
Let's add a method that will get the database connection when we use my Escuela.
We need these data here in order to connect to the database.
For now let's just copy that ease.
And paste them in here.
And later on we'll take these data out of here.
So they're not hard coded
to connect to a database using PDA.
We create a pdl object passing in the connection parameters.
The first parameter is the DSN.
Our data source name which is a string that specifies the type of database to host the database name
and so on.
Then we add the user name and password.
So the DSN string for our database will look like this for my esky Well armoury ADP which specify my
Eskew L..
Then the host followed by the database name and finally will also add the charset option which tells
Pedia how to use the utf Eight character set with this database.
Then we can create a new pdl object passing in the DSN the user name and the password we want to return
this object from this method.
So instead of assigning this object to a variable we'll just return it directly like that.
Finally let's add a p p duck comments to the method
and also to the class
before we start using this new class.
Let's protect the new classes folder in the same way we did with the includes folder with this hasty
access file.
So I'll just copy this file.
And paste it in the new folder.