WordPress defines a class called wpdb, which contains a set of functions used to interact with a database. Its primary purpose is to provide an interface with the WordPress database, but can be used to communicate with any other appropriate database.
The $wpdb object can talk to any number of tables, but only to one database at a time; by default the WordPress database. In the rare case you need to connect to another database, you will need to instantiate your own object from the wpdb class with your own database connection information.
$wpdb->get_row()
get_row() returns single row of data from database table.
<?php
$mycustomer = $wpdb->get_row(“SELECT * FROM laundry_customers WHERE ID = 1”);
echo $mycustomer -> customer_name;
?>
$wpdb->get_results()
get_results() returns multiple rows of data from database table if present. It returns 0 if no row is selected.
<?php
$allcustomers = $wpdb->get_results(“SELECT customer_name, customer_id FROM laundry_customers WHERE status = 1”);
foreach ($allcustomers as $singlecustomer ) {
echo ‘<p>’ .$singlecustomer->customer_name. ‘</p>’;
echo ‘<p>’ .$singlecustomer->customer_id. ‘</p>’;}
?>
$wpdb->get_var()
get_var() retrieves single value from database table. It returns NULL if no result is found.
<?php
$customer_count = $wpdb->get_var(“SELECT COUNT(*) FROM laundry_customers;”);
echo ‘<p>Total customer: ‘ . $customer_count . ‘</p>’;
?>
$wpdb->get_query()
get_query() returns an integer corresponding to number of rows selected/affected. Even if the query returns 1 row or 0 row, get_query() returns 1. If there is a MySQL error, then it returns FALSE.
So if you want to get the actual query results, use get_results(), get_row() or get_var()