WordPress has a built-in Data object class for dealing with SQL query of Insert Update Select and Delete database queries. It’s called wpdb as well as this is a get in the path of includes/wp-db.php file. When you are running queries you required always use this added class to execute them. To use $wpdb class you first required to define $wpdb as a global main WordPress $wpdb variable before you can use it. Just place $wpdb added line of wordpress CMS PHP Source code before every global variable declare as $wpdb method:
Functions for running database queries
The $wpdb->query method is used mainly for SELECT as well as DELETE statements but can actually be used for any sql statement.
$wpdb->query – any query method
$wpdb->query($wpdb->prepare ("DELETE FROM $wpdb->user_define_tbl WHERE ID = 1" ));
As you can see, we use the wp->user_define_tbl to reference a custom Database table in our query.
$wpdb->get_var – return single variable
The get_var() method is used for retrieving a single variable from the database
To retrieve an entire row You would like to use the get_row() method. By default the row is returned as an object, but you can add the PRODUCT_ARRAY or PRODUCT_ARR_ALL parameter at the end of your method. The PRODUCT_ARRAY parameter will return an associative array and the PRODUCT_ARR_ALL parameter will return a numerically index array.
$wpdb->get_row( $wpdb->prepare( "SELECT * from $wpdb->posts WHERE ID=1"), PRODUCT_ARRAY );
$wpdb->get_results – return multiple rows
To retrieve multiple rows from a Database table You would like to use the $wpdb->get_results() method.
$wpdb->get_results( $wpdb->get_results( $wpdb->prepare( " SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' "));
This wordpress select query in wordpress will fetch all the published all the available group of the posts as Data an object.
$wpdb->insert – Insert query
insert query in wordpress for custom table The are specific wordpress wpdb methods for an insert or update sql. The basic usage of the insert() method
$wpdb->num_rows – number of rows returned
We use the num_rows() methods to return the number of rows returned by a sql query.
$wpdb->print_error() – display sql errors
When dealing with usage functions it can be hard to debug what’s wrong with your sql. The print_error() method will display the errors of your sql to the page
$wpdb->get_row( $wpdb->prepare(" SELECT * FROM $wpdb->posts WHERE ID = 3000"); // unexisting ID
The show_errors() method must be called before executing a sql query. The print_error() method directly after running the query.
$wpdb->last_query – print last query executed
The last_query() method will print the last sql query executed. This can be useful to test why a query isn’t working as expected.
var_dump( $wpdb->last_query );
update query in sql
$wpdb->update – update query : update query in wordpress with where clause
We do a update query in a similar fashion. I first of all put our Database table, and then data pass our Column values in an array and then put our where clause variable.
$wpdb->update( $table, $data, $where );
The where variable is an array of Column names and Database table Column values for the where clause. In this example We shall use a unique ID in our mysql where clause.
I hope you get an idea about select Insert Update and Delete query in wordpress.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.