From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
0:0 Welcome to mySQL part 5. To echo our data to the user and to display the results from this, we'll need to use "while" statement.
0:12 As I mentioned, we have created a row variable and this is "=mysql_fetch_assoc".
0:19 This is creating an associative array from our "extract" query which is here.
0:25 So we are selecting everything in the "people" table and ordering them by "id" in ascending order.
0:33 Inside our WHILE since we wrote row as an array and its an associative array, row[0] will be wrong because these are numeric.
0:46 These are numeric id tags and instead of this we'll use our fieldnames, since this is associative.
1:00 So, instead of 0 1 2 3 4, we'll be using the actual name.
1:07 Lets create variables from this. Let me say id and then firstname equals, we are using the same structure throughout.
1:16 So its quite easy to copy and paste this.
1:19 Lets indent this.
1:24 So we have 5 altogether.
1:28 So that's five and then lets just change. This is a lazy act!
1:33 But its just a lot quicker to do this way.
1:36 So lastname and we have the date of birth. We also have the gender.
1:47 We have all our data and now how do we use this?
1:51 We need to use the "echo" command.
1:55 There might be a loop inside the middle, at the moment. So anything we echo out will be repeated.
1:59 Every record we have and that's right, too. We'll repeat this code.
2:03 For example, I say text here. There are currently 4 records.
2:12 After refreshing this page, you should see text echoed out 4 times.
2:17 By typing out 4 times, this piece of code here represents every loop.
2:23 Therefore we can incorporate for example, id or firstname or any other, that we have extracted from the database using our associative array.
2:33 Now I'll write firstname lastname was born on dob for date of birth and is and I'll put gender up there.
2:49 Not forgetting our linebreak. I'll refresh our page.
2:55 Then we have our set of data structured using the variable names.
3:02 We have given in the correct order and also it has been repeated through for every record we have.
3:06 Okay, we have just given out the content of our table using this star, declared by this asterisk, where it collects every single data or every record.
3:20 Now let me do this. I'll say IF gender==F then gender=female.
3:36 The actual spelling of that and then let us say else gender=male. This is just rewriting the variable depending on the value.
3:50 If we refresh now, we can see this has changed to male male and female female. We also have some interesting ways of displaying this data.
3:59 At the moment I'm selecting from the people table and ordering by id and ascending order.
4:06 I can also order by descending id. You can see that this switches this data around.
4:16 We can also order it by firstname. This will put this in descending alphabetical order and ascending will put this in ascending alphabetical order.
4:32 So we got A D E and K.
4:34 You can do the same with the surname.
4:36 You could do the same with anything. Even Date of birth, as long as you include this over here.
4:44 Another thing to do is, let me just take this back to id and have this as descending. We can use this limit 1 or we can say limit 2, 3 or 4.
4:57 Now I'll limit 1 for the purpose of this.
4:59 Now lets have 1 to let the user of the page know the last person that was inserted into this table.
5:10 So I say "echo" here.
5:13 echo last person to be inserted into table was and I'll leave it like that and add a linebreak.
5:28 I'll just echo out the first and last name. Ok?
5:33 So, here we can see that there is a lot of confusion.
5:37 Last person to be inserted. Yes, in fact it does work!
5:40 It is already typed in the "limit" command.
5:42 What I've done is by limiting this by 1 in descending order of id - the id is incremental - I get 4 at the top and if we are limiting it by 1, 4 will be the only record that is selected.
6:02 Therefore, the last person in the table, as per the last record displayed, will have its value echoed out.
6:09 This "while" will only return 1 data value.
6:12 Since we are returning 1 data value here, we are confused by these.
6:17 This is one command here, "select * from people", "order by id decs" is another and "limit 1" is yet another.
6:26 We don't use commas or anything. This is just how we write our code inside our query.
6:36 Okay, just to test this code, I'll insert just using the "insert" function in php myadmin and I'll insert another record.
6:44 For example, lets type in "David Green" and our date of birth could be random.
6:53 It doesn't really matter what we type in here. We say male.
7:00 I come down here and submit this data.
7:02 Click on browse and we have a new value here.
7:03 When we come back here and refresh, that will change to "David Green".
7:11 So this is really useful if you have a website in which your putting videos or personal pictures.
7:18 You can just place in the last thing the user had inserted.
7:21 Or may be the last person that has been registered on your website or anything.
7:26 Possibility of using this is endless.
7:28 Basically how to echo out data and how to manipulate it by just using mysql query.
7:35 In the next part, we will allow our user to specify which data they want to show.
7:45 We will create some html forms and to enable them to do this.
7:50 This will let them select a name from the database or table of their choice.
7:55 So, join me in the next part.
8:03 Bye for now. This is Juanita Jayakar dubbing for the Spoken Tutorial Project

Contributors and Content Editors