Now that we've connected to the database from HP we can query it for some data.
First let's create a variable with the ESC U L statement that we want to use.
You could put this all on one line but I've split it up so it's easier to read and it's perfectly okay
to do this with strings in p p.
And note the semicolon at the end of the school is optional here.
Next we need to send this esky Well query to the database will use this.
My Escuela query functioned for that and again will use the procedural style for now as that's what
we've been using up to this point.
So we'll call this function passing in the connection and the ESC U L and it will return some results
to us.
These results will be the boolean value false.
If there was an error.
So we need to check for that.
We could do that like this using the HP comparison operator.
We learnt earlier.
However there is a potential problem with this.
If we compare a value to false using this operator the following values are also considered to be false.
An empty string.
An empty array and so on.
To specifically check for the value false we need to use the identical comparison operator here which
is three equal signs.
So if we change the code to use that we can be sure that will only enter this cold block.
If there is actually an error in that case let's print out the text of the error message using the my
well error function.
If there isn't an error we want to get the result set.
Now you might see some code where the results are retrieved and processed one at a time using the fetch
function and some people prefer to do that.
However I think it's much clearer to fetch all the results at once into an array using the fetch all
method as that where you dont have to check for the end of the resultset and your not mixing code that's
retrieving results with presentation code.
So will call the fetch I'll function passing in the results.
Variable and this will return an array with the resultset from the database.
So for now let's just dump that out like that.
If we try that out in a browser we get an array containing data that's come from the database.
If we view the source it's easier to see the structure of the array.
It's an array of five items.
So five riccarton the database each one of these represents an individual rijkaard the indexes of these
records and numbers though.
So we don't really know what these fields are.
There's a second argument we can pass to this function which will make this return an associate of a
ray instead.
Now in the browser the results have indexes that correspond to the column names from the database idae
title content and so on and of their corresponding values.
Finally let's just check that our error handling code is working.
If I introduce an error into this esky well like that I've spelt the key word select wrong in the browser
we get an error message.
It's important to pay attention to the text of the message as it will help you to find out exactly what
the error is.
In this case it's saying we have a syntax error in our escudo.
You can correct that.
But this time replace it with another error.
This time in the table name.
Now in the browser we get another error message saying that this table doesn't exist so the error message
text will help us to correct the error.
Let's just fix that.
And our code is working properly again selecting data from the database.