Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the last movie, we learned how to create and close a MySQL connection. But we haven't done anything with that connection yet. We need to be able to issue queries from MySQL and then work with data that it brings back to us. And that's what we'll do in this movie. To do that, we're going to learn three new functions. They're MySQL API functions. They're mysqli_query, which is, of course, what we'll use to do our querying. Then if we get results back, we'll use mysqli_fetch_row. To work with those results, and then as we listed as one of our key steps, we want to free the result at the end. To tell PHP that it's okay to flush that memory and not try to hold onto those results any longer.
That'll free up some memory for us. Alright, so let's try and add these to the databases.php file that we started in the last movie. So, let's open up databases.php, and you can see I've already got my connection up here at the top. And I'm closing it down here at the bottom. Now, I need to actually do my query, and I'm going to do it right here in a new block of PHP. It doesn't have to be. Step number 2, perform the database query. So, we know how to write MySQL. Let's just start by creating ourselves query equals, let's write our string. Select all from subjects. So, there we go. That's simple MySQL that we learned in the last chapter. This should return a list of all columns from all subjects that we have in the database.
Now ,my advice is to always define your query as a separate variable like this. It's really handy. Because then if you want to try to debug it later, it's very easy to just echo that query to the browser. If you're constructing something very complex, you could just say echo query. Find out what you were trying to send to MySQL that had some problems. Now, we've defined this string and set it to the variable but we haven't sent it to MySQL. So, let's do that now using mysqli_query. And then, we need to tell it two things. We need to tell it the connection to use. So, that's my connection variable and query.
Now, this is the reverse of the MySQL API, if you're used to using the older one. Connection used to be the second argument, and query was the first one. They flipped it around for mysqli, so MySQLi is going to use them in this order for connection, followed by the query. Now notice that there's no semicolon at the end of my query string. It's needed here because issuing one and only one query, so there's no ambiguity about it. It knows to end it at the end of this query. Now, we want to catch the results that come back from query so we'll do that, and result, and result is going to be a special kind of thing.
It's not just going to be an array that comes back to us, it's going to be a special kind of object called a resource. And a resource of database rows that we're going to need to then access. Before we do that though, let's first test to see if the query succeeded or not. We can do that by saying if there was not a result then we know we had a problem. We can just do our die again. Die database query failed. Now, failure is not the same thing as not getting any rows back. Let's go ahead and go over and try this real quick.
So, I'm just going to do localhost databases.php again. Reload the page. And you'll see that it worked fine. We didn't display anything, but we didn't get an error either. Now, just try this select. Let's take out the L, right? That's no longer a valid MySQL. Let's save it. Reload the page, database query fail, so bad syntax will give you a failure. Let's try something else. Let's try find all from subjects where id equals 2000. There are not 2000 subjects in our database, so we definitely won't get anything back. You see, we don't get an error. See the difference? So an error really means, in this case. So there's no result that there was a database query error. Something really went wrong.
So, let's just put a note here to remind ourselves. Test if there was a query error. Okay, so now, we have back our results. So, our subjects are here in $result. In a resource set that we can then work with. The way that we do that is that we need to use MySQLi fetch row. I'm going to do that in the body down here. Now, you could do it up here and assign it to an array or something like that. Oftentimes, this is what happens in the body. This is the step that we do here because we're actually going to do our outputting here.
So, let's just make a note here. This is step three. Use returned data, if any. So, how are we going to do that? Well, we're going to use a woe loop to loop all the rows it returned to us. Our wow loop will output and for each one for now, let's just output data from each row. Let's just do var_dump on it to begin with. So var_dump, whatever we get back. And then at the end, I'm just going to put an HR tag, just a horizontal line across. All right, so what we want to do is just dump the output of each row.
So, the while statement here, while something is true, this is the tricky part that you want to pay attention to. Row equals mysqli_fetch_row from our result. So, what this does is says go get the first row from that result set, and assign it to row. Notice this is a single equals and not a double equals. This is not a conditional. While often takes a conditional. While something is true. What this is saying is, while you're still able to bring something back and assign it to this, then do this. As soon as there's nothing left, well then what gets assigned to row? Null.
Null gets assigned to it. There's nothing left. And therefore, the loop exits. Now, I hinted at this technique at the end of chapter seven, when we discussed array pointers. This is how we work with MySQL result sets. And it's because fetching the row increments the array pointer for us. PHP doesn't have the ability to increment the array pointer using a simple PHP loop. Because it's not a PHP array, it's a MySQL result set, it's different. So, this will increment the array pointer for us. We don't have to do it, all we have to do is fetch it.
Just keep pulling another row off the front. And every time we pull a row, we now have moved the array pointer down the set to the next one. And when we get to the end, then the loop will exit. So, for example, you can't use for each with this. You can't use for each MySQLi fetch row result as row. Because that's going to try and increment the pointer at the end. And we can't. We don't have the ability to. The only way to do it is this way, using this syntax, using while. So now that we're doing this, let's take a look. Let's wee what we've got, let's go back over to our browser, let's reload. And there we go, we're getting a dump from each one, and you can see, here's About Widget Corp, here's Products, here's Services. We're seeing all three of our subjects.
Now, I'm just dumping in in a pretty ugly way. We'll clean that up a little later. But you're able to get data back out of the database, we're able to fetch it. Now, I think this is a good place for me to show you a common technique that I use and a lot of developers use. Which is that instead of just having this simple query here, we can actually assemble a query. And let me show you what I mean by that. I'll just skip some lines here. And instead of just saying select all from subjects, I'm going to put some double quotes here, semicolon return, query. Notice, I'm concatenating it together. So, query equals select.
With a space, and then I'm concatenating from subjects. So, it makes it nice and easier to read, especially when we start having more things to add. So, for example, then we say, query, then we'll concatenate. Where visible equals 1, space, space is very important. I need a space here as well, from subjects. If I don't, then what happens when this gets concatenated together. This S and W are right next to each other. I need a space after each one and then where visible equals 1, query equals order by position, ascending. I call it assembling a query.
That's a very common technique to do something like this. And then you can also wrap things in if, else statements. If something is true, then we're going to append the where clause. Otherwise, we won't. So, let's just save that. And let's come back over here. And at the very bottom, the last thing, remember, that we need to do is we need to release the resource. So that MySQL can do something else with it, and you can do that anywhere. As soon as you're done with it, you can release it so you could do it right here, right after we're done with it. Before we do the body and everything, or we could do it at the end when we close. I'm going to go ahead and just put it here.
Step 3, release the returned data when we're done with it, and then step 5, close the database connection. So that's it. We have all of our steps now. Notice we have step one, create the database connection, step 2, perform our query. Then down here, use the returned data, release the returned data, and then close database connection. And as I said, you can repeat steps 2, 3, and 4. You might then want to make another query to get back the pages that belong to each subject. So maybe while we're looping through these, we stop, and right here we have another query that will query for the pages and output all of the pages.
The main thing I want you to understand is just the overall flow and the overall structure of connecting the database. Connect, make your query, use the data, release the returned data and then close the connection at the end. In the next video, I want to spend a little bit more time looking at this MySQLi fetch row that we did. Because there's some neat options that we can use here.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101019 Viewers
61 Video lessons · 87812 Viewers
71 Video lessons · 71686 Viewers
56 Video lessons · 103575 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.