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.
As I mentioned, we have created a row variable and this is "=mysql_fetch_assoc".
This is creating an associative array from our "extract" query which is here.
So we are selecting everything in the "people" table and ordering them by "id" in ascending order.
Inside our WHILE since we wrote row as an array and its an associative array, row[0] will be wrong because these are numeric.
These are numeric id tags and instead of this we'll use our fieldnames, since this is associative.
So, instead of 0 1 2 3 4, we'll be using the actual name.
Lets create variables from this. Let me say id and then firstname equals, we are using the same structure throughout.
So its quite easy to copy and paste this.
Lets indent this.
So we have 5 altogether.
So that's five and then lets just change. This is a lazy act!
But its just a lot quicker to do this way.
So lastname and we have the date of birth. We also have the gender.
We have all our data and now how do we use this?
We need to use the "echo" command.
There might be a loop inside the middle, at the moment. So anything we echo out will be repeated.
Every record we have and that's right, too. We'll repeat this code.
For example, I say text here. There are currently 4 records.
After refreshing this page, you should see text echoed out 4 times.
By typing out 4 times, this piece of code here represents every loop.
Therefore we can incorporate for example, id or firstname or any other, that we have extracted from the database using our associative array.
Now I'll write firstname lastname was born on dob for date of birth and is and I'll put gender up there.
Not forgetting our linebreak. I'll refresh our page.
Then we have our set of data structured using the variable names.
We have given in the correct order and also it has been repeated through for every record we have.
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.
Now let me do this. I'll say IF gender==F then gender=female.
The actual spelling of that and then let us say else gender=male. This is just rewriting the variable depending on the value.
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.
At the moment I'm selecting from the people table and ordering by id and ascending order.
I can also order by descending id. You can see that this switches this data around.
We can also order it by firstname. This will put this in descending alphabetical order and ascending will put this in ascending alphabetical order.
So we got A D E and K.
You can do the same with the surname.
You could do the same with anything. Even Date of birth, as long as you include this over here.
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.
Now I'll limit 1 for the purpose of this.
Now lets have 1 to let the user of the page know the last person that was inserted into this table.
So I say "echo" here.
echo last person to be inserted into table was and I'll leave it like that and add a linebreak.
I'll just echo out the first and last name. Ok?
So, here we can see that there is a lot of confusion.
Last person to be inserted. Yes, in fact it does work!
It is already typed in the "limit" command.
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.
Therefore, the last person in the table, as per the last record displayed, will have its value echoed out.
This "while" will only return 1 data value.
Since we are returning 1 data value here, we are confused by these.
This is one command here, so lets" * from people", "order by id decs" is another and "limit 1" is yet another.
We don't use commas or anything. This is just how we write our code inside our query.
Okay, just to test this , I'll insert just using the "insert" function in php myadmin and I'll insert another record.
For example, lets type in "David Green" and our date of birth could be random.
It doesn't really matter what we type in here. We say male.
I came down here and submit this data.
Click on browse and we have a new value here.
When we come back here and refresh, that will change to "David Green".
So this is really useful if you have a website in which your putting videos or personal pictures.
You can just place in the last thing the user had inserted.
Or may be the last person that has been registered on your website or anything.
Possibility of using this is endless.
Basically how to echo out data and how to manipulate it by just using mysql query.
In the next part, we will allow our user to specify which data they want to show.
We will create some html forms and to enable them to do this.
This will let them select a name from the database or table of their choice.
So, join me in the next part.
Bye for now. This is Juanita Jayakar dubbing for the Spoken Tutorial Project