
Last week we began covering the WPDB class available in WordPress. This week we will continue along that topic. WordPress provides multiple functions for doing SELECT queries on the database.
Generic Select
With the WPDB class, there are functions available to select specific variables, rows or columns, but there is also a “generic” SELECT. get_results() allows you to perform a SELECT query on the database, and have the results returned to you in an object, associative array or numerical array. This syntax of get_results is simple:
[code='php']$wpdb->get_results('query', output_type);[/code]
Where query is your SELECT query and output_type is either:
- OBJECT – result will be an object (default)
- ARRAY_A – result will be an associative array
- ARRAY_N – result will be a numerical array
Selecting a Row
If you want to retrieve an entire, single row, the WPDB class has a function for this. get_row() will output one row and cache the rest of the result.
[code='php']$wpdb->get_row('query', output_type, row_offset);[/code]
query is your SELECT query, output_type is the same as get_results() and row_offset is the row you want (starting from 0, which is the default).
Selecting a Column
Similar to get_row(), get_col() will return a single column from your SELECT query.
[code='php']$wpdb->get_col('query', column_offset);[/code]
query is your SELECT query and column_offset is the column you would like return (starting from 0, which is the default).
Flushing the Cache
Whenever you run a query using a function in the WPDB class, related information is cached in the variables $wpdb->last_result, $wpdb->last_query and $wpdb->col_info. You can empty this cache (and you should after you’re finished with it) using the flush() function.
[code='php']$wpdb->flush();[/code]
Next week
Next week, we’ll be covering how to use the WPDB class to insert and update data.




