PHP-and-MySQL/C4/User-Login-Part-2/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
0:00 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.
0:14 Now we’ve already connected to our database.
0:18 Refreshing this and resending my username and password, we can establish that there have been no errors.
0:24 I mean this error here.
0:25 And we have seen that if we don’t type data we will get an error.
0:28 Now. First of all I’m going to setup a query.
0:36 If you have used "mysql" or any structured query language before, you will know that you can query a database.
0:43 I think Microsoft Access has this.
0:46 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.
0:54 I don’t think we need the id but "SELECT *" anyway so that it will grab all the data.
0:59 So "SELECT * FROM" and we’ve called this, users, I think. Let me confirm that.
1:04 Yes, users. So "SELECT * users" and here we’ll say "WHERE username" which is the name of this...... this, here.
1:20 And we will say "WHERE username equals" the "username" that has been typed.
1:30 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".
1:37 So what we will do is we will use another function, a mysql function called "mysql num rows".
1:46 This counts the number of rows that have been retrieved from the query you’ve given the database.
1:53 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.
2.08 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.
2.16 Let me click insert and add another row of data, for example - another username and another password.
2:26 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.
2:38 Okay, lets try that. And there we are. Hold on a second. Where am I? Ah there we go.
2:53 So we have got "Alex" and "Kyle".
2:55 We can see the ids have been automatically incremented.
2:58 You can see both our passwords here and the 2 usernames.
3:02 Now we will refresh this and see what we get.
3.06: Oh Ok. This is the whole part of the check.
3:10 The reason this has returned 1 is that if I were just selecting every user and then counting the row, the value would increment.
3:18 Go back here and refresh and we’ll get the value of 2 because there are 2 rows.
3:22 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.
3:34 Usually on a website, you won’t have duplicate username.
3:40 Okay. So now that we have got this, what is the purpose of finding out how many rows there are.
3:47 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.
4.01 Otherwise, sorry "else", we need to echo out, sorry "else die". We will give the message "That user doesn’t exist".
4:16: So what we’re doing is, we’re checking that a row has been returned where we have supplied that username.
4:25 And if it doesn’t equal zero, we can execute our code to login.
4:29 Otherwise we’ll say die and "Tthat username doesn’t exist".
4:33 This will be equal 1, 2, 3, 4 and so on.
4:40 If it doesn’t equal zero, then it must equal something.
4:44 And if it does equal something, then the code in here will be executed.
4:47 So if it is equal to 0, that basically means that no result can be returned.
4:52 I’ll resend this. Let’s go back.
4:57 And......... let’s get rid of our "echo num_rows".
5:05 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.
5:13 Nothing’s happened because no errors have been returned.
5:15 Now let me use Billy, for example, and type the password in and click on login.
5:21 "That user doesn’t exist!" because no rows that have the username equal to Billy have been returned.
5:26 So we can see that that’s working.
5:28 I’ll go back to my original thing.
5:31 So "Alex" and my password is "abc".
5:37 Now the code to login.
5:39 Inorder to login, we need to perform a password check.
5:42 So to grab the password, I’ll use a function.
5:46 Sorry not a function, I’ll use...... a loop and that loop will be the "while" loop.
5:52 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?
6:11 So I’ll say "mysqul_fetch_assoc" for short.
6:22 And this is going to be my query. So I’ve got my query there.
6:28 From this, we are fetching each column data from here and putting it into an array called "row".
6:40 So obviously with the while loop, we’ll have our brackets and now we’ll set some variables.
6:45 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".
6:55 So as we can see here, this is the row name here.
6:59 If this is an array of the data, then each of these are going to be id, username and password.
7:06 We’re not using 0,1,2. But I’m not sure whether that works.
7:10 Now we’ll keep it simple and we’ll directly reference the name of our column.
7:20 So the database username will be "row" and that since this is an array which is using this function on our query.
7:26 Next we will say "db password equals row" and then our password.
7:38 So after this we could echo out....
7:43 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.
7:49 We already know what they are. We’ve seen them in the database.
7:51 Now what we’ll do is we will perform a check. So "check to see if they match".
8:00 It’s very simple to do this using an "if" statement.
8:04 "if" our username equals our db username and our password is equal to our db password , then we’ll say it’s correct.
8:19 Otherwise, we’ll say it’s incorrect.
8:22 I will remove the brackets because there is only one line. So echo "Incorrect password!". Just leave it like that.
8:34 And here we’ll say echo "You’re in!".
8:41 Okay we’ll just test this before I end this part of the video.
8:46 I’ll first say "Alex" and I’ll put a wrong password in. "Incorrect password!".
8:51 And now I’ll put "abc" as password and "You’re in!".
8:55 So we’ve checked our username and it exists.
8:58 We’ve checked our fields exist so please enter in your username and password.
9:04 If we enter in a username and a wrong password we get an error message – "Incorrect password".
9:11 If we enter the correct password, we get- "You’re in".
9:13 And if we enter a username that is not found, we get an error message saying the user doesn’t exist.
9:24 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!
9:32 This is Antara dubbing for the Spoken Tutorial project.

Contributors and Content Editors

Chandrika, Kavita salve, Pravin1389