$wpdb Object usage Examples as used in the WPDB Power Object
These are all of the current $wpdb Object usage Examples of methods and properties used by the WPDB Power Tool. Added as reference should you utilize this for database development in a WordPress, Theme, Plugin or Template. There are more detailed explanations of the $wpdb object at developer.wordpress.org
The get_col_info method does just that. The info it can get (single parameter to pass) are ‘name’, ‘table’, ‘def’, ‘max_length’, ‘not_null’, ‘primary_key’, ‘multiple_key’, ‘unique_key’, ‘numeric’, ‘blob’, ‘type’, ‘unsigned’, ‘zerofill’. Example usage:
<?php
$sql = 'SELECT * FROM my_posts';
$wpdb->get_results($sql);
$colNames = $wpdb->get_col_info('name');
foreach($colNames as $name){
echo $name . '<br />';
}
?>
Designed to return a queried recordset. It still has to be parsed through to display. Here are a couple of ways to do this.
First Method Row by row getting each field:
<?php // This method allow you to parse througth rows by field name:
$sql = 'SELECT * FROM my_postmeta;';
$records = $wpdb->get_results($sql);
foreach($records as $key => $row){
echo 'meta_id ' . $row->meta_id . '<br />';
echo 'post_id ' . $row->post_id . '<br />';
echo 'meta_key ' . $row->meta_key . '<br />';
echo 'meta_value ' . $row->meta_value . '<br />';
echo '<hr />;
}
?>
Second Method:
<?php // This method allow you to parse through rows by fields:
$sql = 'SELECT * FROM my_postmeta;';
$records = $wpdb->get_results($sql);
foreach($records as $key => $row){
foreach($row as $field => $value){
echo $field . ' ' . $value . '<br />';
}
echo '<hr />'; // Separate each record with a line break.
}
?>
The get_var method returns a single value. It is used for a single row singe column. Example:
<?php $sql = "SELECT post_content FROM my_posts WHERE id = 8;"; $content = $wpdb->get_var($sql); echo $content; ?>
The last_error property stores the last error or nothing if there was not any errors. In order to get this property the property
$wpdb->show_errors must be set to true. It is useful for displaying MySQL/MariaDB errors and more often than not will not be utilized in a typical web application and will be more applied to debugging and tuning a database rich application web or otherwise. The WPDB Power Tool utilizes this extensively for the end user of this application as this is as much a development tool as it is a maintenance and research utility tool. Example Usage:
<?php
// Will generate an error:
$wpdb->show_errors = true;
$sql = 'SELECT * FROM my_postsmeta;';
$wpdb->query($sql);
if($wpdb->last_error !== ''){
$lastError = $wpdb->last_error;
echo '<b>WordPress database error:' . $lastError . '</b><br />';
echo '<b class="red-font">Generated from SQL <pre>' . $sql . '</pre>';
}
$wpdb->show_errors = false; // Set it back to default when done
// some applications do not care about certain errors.
?>
The num_rows method provides the number of rows on a given record set. Example usage:
<?php
$sql = 'SELECT * FROM my_postmeta;';
$records = $wpdb->get_results($sql);
$rowCount = $wpdb->num_rows;
echo $rowCount;
?>
This method is useful for UPDATE, DELETE or INSERT queries. Or queries that execute without returning a response. Example usage:
<?php
$wpdb->show_errors = true;
$sql = 'DROP procedure IF EXISTS `' . $procExists . '`';
$wpdb->query($sql);
if($wpdb->last_error !== ''){
$lastError = $wpdb->last_error;
echo '<b>WordPress database error:' . $lastError . '</b><br />';
echo '<b>Generated from SQL <pre>' . $sql . '</pre>';
}
$wpdb->show_errors = false; // Set it back to default when done
// some applications do not care about certain errors.
?>
The show_errors method is used for capturing MySQL/MariaDB errors. It is set to false by default. Example usage:
<?php
// Will show generated errors if asked:
$wpdb->show_errors = true;
$sql = 'SELECT * FROM my_postsmeta;';
$wpdb->query($sql);
if($wpdb->last_error !== ''){
$lastError = $wpdb->last_error;
echo '<b>WordPress database error:' . $lastError . '</b><br />';
echo '<b>Generated from SQL <pre>' . $sql . '</pre>';
}
$wpdb->show_errors = false; // Set it back to default when done
// some applications do not care about certain errors.
?>