Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
WordPress is powered by a MySQL database. The main way that we can access this database directly is using the special wpdb class, which is loosely based on EasySQL. It's an abstraction of the SQL language for MySQL, and it does require a little bit of knowledge of Structured Query Language. Keep in mind that while you can access most of the data in the database directly, you probably don't want to do. There are a lot of different functions made there to get options and get posts and pages and things like that. You want to use those where possible.
Eventually, you may be creating your own database tables, and this can be helpful to know. I have a dbinfo.php plugin that I created in our plugins directory, and it's set up to be a widget in the admin. So you can see I've added this dashboard widget function which registers that function, and then I added the action. So I am all set up, just like we learned how to do in the admin widgets video. What I am going to do now is I am going to access the wpdb class, so the first thing I need to do is set it into the global scope.
That allows me to access it at will. Now I am going to output some data. First, I am put a little title, "DB Info Dashboard Widget," and under here--let's make sure to close that out because I am going to be using a PHP in here. And let's go ahead and just output this directly, so we'll just put little pre, and we'll just say php echo. And there is special function called var_dump, which allows you to see the properties of any object, and we'll just pass in $wpdb.
So we'll save it, and let's go into the Plugin screen and activate it, and then we can go to our dashboard, and we should see it up here. So you can see, it's output all this data. We have show_errors, suppressed_errors, last_error, num_queries, num_rows, rows_affected, and all kinds of different information that tell us about the database. All the different fields are available as properties. You can see I have the categories field. I have some other things, the users table, the taxonomy table--all this information I can access.
We don't need to see all that though, but maybe we do want to see a couple of things. So let's add something that will output the last query. So to do that, we simply echo $wpdb-->last_query. I am going to close the paragraph. We'll do another one that will show the last error. It's just for admin users, so they can see what's going on in the database.
Let's go ahead and refresh. So you can see we have our last query, which is something in regards to generating the menu, and the last error, which is nothing, because we haven't had any errors yet. All right, so let's go ahead and get some more information. There's a special query method that will run the query, but it'll returned how many records were affected by that query. And a lot of times we use this for insert and updates and things. But it can be a good way to get a count of something. So Total Users, and then we can just say echo $wpdb-->query and then in here, we can just say SELECT ID from wp_users.
And you can type in the fields directly, although usually you're going to want to use these variables, which I'll show you in a second. So you can see I have two users currently in the database. If you want to access data from the database, there's basically four ways you can do it: You can get a single field from the database, you can get a single column, you can get an entire row, or you can get a whole result set from a query. So if I wanted to just get a single field, I am going to ahead and get the Last post, and we are just going to say echo $wpdb-->get_var, and then I want to say SELECT post_title FROM and then to use table names that are built in, you can just concatenate on the properties, wpdb, and this is going to be in posts, dot.
And then you can add a where clause: WHERE post_author=. Let's got ahead and put this on a new line so we can read it. Make sure you have enough space in between here. Now, I want to get the ID of the current user, and I can do that by globalizing the current user property. So now I can just concatenate in here, the ID. So this will tell whoever's looking at the query the last post that they made, and then we'll close the paragraph. Refresh it.
So the last post was Hello world! which is the only post because we're just using the default installation. When you go into get an entire column, an entire row, or a result set, you are going to use Get call, Get row, or Get results. They basically work the same way where we are going to pass a query, only instead of returning a single value, they're actually going to arrays, and you can control how that's output, and we'll look at how to use those in future videos. So WordPress has the built-in wpdb class that allows us to access the built-in WordPress database.
In addition to the number of properties it has that can give you all kinds of information about the database, it also has a number of methods that allow you to query the database in a number of different ways directly.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98513 Viewers
61 Video lessons · 85807 Viewers
71 Video lessons · 69711 Viewers
56 Video lessons · 102023 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.