Get Data Using SELECT Query

9 minutes
Share the link to this page
Copied
  Completed
The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.

Transcript

Hello, in this video, we are going to look at using this select query to get data. So first of all, I want to say that I'm not really a mistake in the previous video, you're connecting to a database, forgot one online. And this is where you should actually close your connection. So you just do connection equals No, like so. So it's just good practice to close the connection. And any thing regarding connection would go between I know that you've done that should still run, you know perfectly the way it did before.

And at the moment, we've got this table guard database table, no data in it. So we're not going to be inserting data using queries or using code yet that will come later in the video. But for now, I was going to go to the Insert and insert some data here. I can leave the ID empty because that was automatically just increment as we add data. So let's add a username. So I'm gonna say Put my name, click Go.

And we go to browse. As you can see, we've got it there. Let's add a few more to less Batman. So one more. And this can be you. And there we go.

So we have free pieces of data free users. And as you can see the Id just increments one after another. So now to actually get the data, what we want to do is create a query and the query is going to equal to the connection equal to connection. Prepare. So this is where the prepared statements come in, which are a lot more secure than on prepared statements. So this is the way you have to do it in PDO.

This is the reason I love PHP data objects. It forces you to do prepared statements, which this is a different format, but it just essentially strips out certain characters, which you can No, we can get back when you're displaying. And we just makes it more secure. So this is going to be a query. So the query to get data The format is select. This just means, again, data was selected.

Now we specify what we're selecting. So we go here, and we want to get ID and username. So all you do, and then we separate the next value that we want to get username, then we need to specify where we're getting it from. We don't need to specify the database because that's what we've connected to. So all we need to do specify is from then the table name, which is users before typing user, like so. So that just creates the query.

It hasn't run yet to run it. We do query execute. And now we want to get the data from the query. So we can just do Actually one second engineer, so it's easier for me to scroll. And now you want to do results equals query on the set fetch mode, and the mode we are going to be using is PDO. Fetch underscore, a sock, which is fetching it as an associative array.

If you want to learn more about arrays and associative arrays, feel free to go back to a previous video where I cover all of that. So this is just a nice format that we can, you know, use it in. So nervous, you know, Echo this out. And let's see what we get for echo that wants it and I'll want to do that. And I'll do it here as well. We shouldn't begin to add them but just in case you do just a bit of nice extra formatting.

Okay, so we say one. That's what we've got for the result. One second. One second we did it. Or am I missing? Two?

Yeah. Okay, that's fine. The set demoed will slow the query, fetch all and this returned an array, so that's fine. So it's about a loop over the array. What we can do is say for each, and in here, we're gonna create a new table for new Do we need a new table now we don't even need that. So we're just printed out Normally, I would just do dollar query, fetch all and this is going to be as dollar value.

And we'll say echo dollar value. So let's see what we get. So we just got loads of rays still. One second I put this here. It's dead. working the way I wanted to Now that we're going to put new recursive array iterator, so we need an iterator to be able to go over now each single row essentially, and get in.

Okay, so yeah, there are one two for that. Yep, that's fine. And therefore the value we get this which is great, which is what we want. And to get the individual value, you just put that then the name of the column which happens to be username. username Okay, that's why it's not working the way we expected it to. Okay, there we go.

So we got it like this little mistake on my part, we can get the ID. Well, there we go. So let's format this up a little bit. So what we're going to say is hold on the dollar value, username. And we're gonna put a new line of well. There we go.

We have got all the data and we was able to essentially do over our data. So I'm just formatting the code. I like it. And another thing I want to show, so we selected to get ID and username. If I did this and I run it, we'll get an error. Because it's undefined index ID, because we haven't chosen to get ID.

So unless we choose to get that particular column, we won't get the information. If you have multiple columns, and you want to get all of them, you can just play an Asterix, so this just say, get every single column. And that will work the same way as well. So it's, it's great for you and get everything and you don't care how many columns or if you only want to get specific columns, this is the method to go and they don't have to be, you know, in order as in, you have to have like, if I go back here, the ID username, identified one core email address here. I don't have to go ID using an email or Crisco ID, skip username and go for email address. So that's another great thing about it.

Apart from that's really all there is to actually get data over the next few videos, we're going to look at, you know, filtering the data, we're going to look at sorting there and doing a few other cool things as well. But for now, it's really all there is. And what I would recommend as an exit task, put this in its own try catch block, because what you would probably have is this in a separate file. This is like your database connection file, you would have this in another separate php file, which will be at the end of all your pages, and you'll be required into it. And I'm gonna talk about including requiring files at a later date in this later part in this series, and then this will just be used on individual pages. So it's best if you put it in a try catch block, so only as an extra task.

That's why I want you to have a look at doing. So that's it any questions, feel free to pop me a message. And as usual, I look forward to seeing you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.