Welcome to the second part. Here I’ll show you how to edit your "login dot php" page to connect to the database and also how to check our username and password against that in the database.
Now we’ve already connected to our database.
Refreshing this and resending my username and password, we can establish that there have been no errors.
I mean this error here.
And we have seen that if we don’t type data we will get an error.
Now. First of all I’m going to setup a query.
If you have used "mysql" or any structured query language before, you will know that you can query a database.
I think Microsoft Access has this.
So here we are going to say, "SELECT", in fact we’re going to say "SELECT *" because we need the ID, the username and the password.
I don’t think we need the id but "SELECT *" anyway so that it will grab all the data.
So "SELECT * FROM" and we’ve called this, users, I think. Let me confirm that.
Yes, users. So "SELECT * users" and here we’ll say "WHERE username" which is the name of this...... this, here.
And we will say "WHERE username equals" the "username" that has been typed.
Now if that "username" doesn’t exist, we need to display some kind of an error message to say that "This user doesn’t exist".
So what we will do is we will use another function, a mysql function called "mysql num rows".
This counts the number of rows that have been retrieved from the query you’ve given the database.
So we’ll say "numrows equals mysql_num_rows" and in the brackets we have the name of our query, the variable that I have stored the query function in.
And if we echo out the number of rows, I’ll just prove to you and test for myself that we should get 1 because we have just 1 row.
Let me click insert and add another row of data, for example - another username and another password.
I’ll just try that now. I’ll be testing this later on. Lets see, lets say username is "Kyle" and password is "123" this time.
Okay, lets try that. And there we are. Hold on a second. Where am I? Ah there we go.
So we have got "Alex" and "Kyle".
We can see the ids have been automatically incremented.
You can see both our passwords here and the 2 usernames.
Now we will refresh this and see what we get.
Oh Ok. This is the whole part of the check.
The reason this has returned 1 is that if I were just selecting every user and then counting the row, the value would increment.
Go back here and refresh and we’ll get the value of 2 because there are 2 rows.
But if I’m saying "SELECT where the username equals my username", we’re obviously only selecting where my username exists and that is in 1 row.
Usually on a website, you won’t have duplicate username.
Okay. So now that we have got this, what is the purpose of finding out how many rows there are.
Now the purpose is that we can say "if num_rows is equal to zero", then that means we can.. Sorry, if "my num_rows doesn’t equal zero", then we can execute the code we need to do, that we will need to login.
Otherwise, sorry "else", we need to echo out, sorry "else die". We will give the message "That user doesn’t exist".
So what we’re doing is, we’re checking that a row has been returned where we have supplied that username.
And if it doesn’t equal zero, we can execute our code to login.
Otherwise we’ll say die and "Tthat username doesn’t exist".
This will be equal 1, 2, 3, 4 and so on.
Sorry it will equal...
If it doesn’t equal zero, then it must equal something.
And if it does equal something, then the code in here will be executed.
So if it is equal to 0, that basically means that no result can be returned.
I’ll resend this. Let’s go back.
And......... let’s get rid of our "echo num_rows".
Okay. So let’s go back to our main page and we’ll login with "Alex" and "abc"; password doesn’t matter at the moment.
Nothing’s happened because no errors have been returned.
Now let me use Billy, for example, and type the password in and click on login.
"That user doesn’t exist!" because no rows that have the username equal to Billy have been returned.
So we can see that that’s working.
I’ll go back to my original thing.
So "Alex" and my password is "abc".
Now the code to login.
Inorder to login, we need to perform a password check.
So to grab the password, I’ll use a function.
Sorry not a function, I’ll use...... a loop and that loop will be the "while" loop.
I will type a variable name here. I’ll call it "row" and that is equal to "mysql"..... "mysql_ fetches a row as an array". Okay?
So I’ll say "mysqul_fetch_assoc" for short.
And this is going to be my query. So I’ve got my query there.
From this, we are fetching each column data from here and putting it into an array called "row".
So obviously with the while loop, we’ll have our brackets and now we’ll set some variables.
I’ll say "db username", which is the username that I’ll extract from the database, is equal to the "row" and this is the row name, "username".
So as we can see here, this is the row name here.
If this is an array of the data, then each of these are going to be id, username and password.
We’re not using 0,1,2. But I’m not sure whether that works.
Now we’ll keep it simple and we’ll directly reference the name of our column.
So the database username will be "row" and that since this is an array which is using this function on our query.
Next we will say "db password equals row" and then our password.
So after this we could echo out....
No, in fact we don’t need to echo out our contents of our db username and password unless we want to run into errors.
We already know what they are. We’ve seen them in the database.
Now what we’ll do is we will perform a check. So "check to see if they match".
It’s very simple to do this using an "if" statement.
"if" our username equals our db username and our password is equal to our db password , then we’ll say it’s correct.
Otherwise, we’ll say it’s incorrect.
I will remove the brackets because there is only one line. So echo "Incorrect password!". Just leave it like that.
And here we’ll say echo "You’re in!".
Okay we’ll just test this before I end this part of the video.
I’ll first say "Alex" and I’ll put a wrong password in. "Incorrect password!".
And now I’ll put "abc" as password and "You’re in!".
So we’ve checked our username and it exists.
We’ve checked our fields exist so please enter in your username and password.
If we enter in a username and a wrong password we get an error message – "Incorrect password".
If we enter the correct password, we get- "You’re in".
And if we enter a username that is not found, we get an error message saying the user doesn’t exist.
Okay so join me in the next part, and I’ll show you how to create your sessions and your log out page. See you then!
This is Antara dubbing for the Spoken Tutorial project.