Now we have a database table where we can still user data.
We can replace this code in the User class that contains a hard coded username and password.
First let's add a ricard to the user table.
We'll let the database assign the idea so we'll leave that column blank and we'll just supply the username
password and click go and there's the user rijkaard.
Back in the authenticate method in the User class we can now remove this line and get the user data
from the database instead.
First Well at some ESC you l to select the user record with a place holder father user name then as
this is a prepared statement.
We prepare it which requires the database connection so thats out that as an argument to the method.
And also to the comment block.
Then we bind the value of the user name argument to the user name parameter which we blind as a string.
Then we execute it and we won't bother checking if this returns true are not as if there's an exception
we can deal with it later then we'll fetch the results into a variable like that by default This returns
an array but as we're dealing with objects now we want an object instead.
So let's set the fetch mode as we do it with the article class to return an object of this user class.
So now the fetch method will return an object if a record is found and false.
Otherwise if an object is returned will next check the password and if it matches return true.
If we don't return anything from this method nold will be returned which still works in an IF statement
as its boolean equivalent is false so we don't need to return false.
If the username our password are wrong.
Let's update the comment though like that.
Then in our logging script we need to pass in the database connection object to this method and for
that we need to require the database class at the top and then create a database object and get the
connection.
Let's give that a try.
First without any credentials and that doesn't our case as expected.
If I get the username wrong
it also works and if I get the password wrong
as well if I put the correct username and password.
It Arthur indicates successfully using the rijkaard in the database instead of hard coded values.
We can actually clean this code up a bit instead of assigning the result of the fetch method to the
user variable then checking this in an IF statement below.
We can do this in just one line
assigning the variable at the same time we check its value
on the next line.
We're checking the Bula in value of this statement.
Then returning.
True.
If it's true so we can just return this directly instead like that.
And this still works as before
logging goes in successfully.