Now that we have our database structured and containing data we can start using it from HP.
First we have to connect to it a database server like my esky Well Maria idb runs separately to the
web server it can run alongside the web server on the same computer.
Like what you get on your local computer when you install that stamp it can run on a separate computer.
This is quite common when you come to copy your website to a production server.
The web server will be on a different computer to the database server.
To connect to a database from HP you need to know for pieces of information the address of the database
server known as the host.
This will normally be a local host on your local computer.
The name of the database the username and the password.
We created all these when we created the database in P P might add in earlier.
To connect to a my Eskew well on a read dbe database in P P to begin with will use the my Escuela connect
function.
This is one of the many functions provided by my esky Well improved extension which is part of HP by
default.
Even though this explicitly mentions my Eskew L. It works seamlessly with Myriad's B.
We'll start by creating four variables with the necessary connection data the host which in our case
is local host as it's the database server running locally.
The database name which I called See amass the user which we created when we created the database.
I called it C.M. s w w w and the password which we generated after Mattingly when we created the user.
Then we call the.
May I ask you will I connect method passing in these data which connects to the database and returns
a variable which represents the connection to the database server.
To check the connection has succeeded.
We can call this Kinect error function that returns a string containing an error message.
If it fails otherwise it returns NULL.
Now you might notice the documentation refers to object oriented and procedural style.
For now I will stick to the procedural style as that's what we've been using so far.
Later on in the course we'll see how to use Object Oriented code.
We can call this in if statements like this.
We can do this because a none empty string will be cast to true will be converted to the built in value
true and null will be cast are converted to the boolean value false.
So if there is a connection error the code will go into this code work.
In here we can print out the connection error which will give us more details about why the connection
failed and then will call exit which stops the script right where it is.
If the connection fails we dont want to continue executing the script outside the f block.
If at this point we've connected successfully let's print out a message to that effect like that.
When we look at this in a browser we get the message saying we've connected successfully.
This is because we've got all the connection details correct.
If we change one so that it's wrong
and then go back to the browser we get a connection error message like this.
You get this.
Then you need to check and correct your connection data.
This is a useful script on its form.
If you ever need to check that you have the correct database connection data.