Getting started with WordPressget_bloginfo()Enqueuing scriptsMaking network requests with HTTP APIEnqueuing Styleshome_url()Custom Post Typestemplate_includeThe Loop (main WordPress loop)AJAXThe $wpdb ObjectActions and Filterswp_get_current_user()Add/remove contact info for users with user_contactmethods filter hookCreating a custom templateCustomizer Hello WorldCustomizer Basics (Add Panel, Section, Setting, Control)The Admin Bar (aka "The Toolbar")Querying postsAlternating main loop (pre_get_posts filter)ShortcodeCreate a Post Programmaticallyget_template_part()Taxonomiesget_template_part()ShortcodesPost FormatsCustom exerpts with excerpt_length and excerpt_morePlugin developmentSecurity in WordPress - EscapingTemplate hierarchyRemove Version from Wordpress and StylesheetsChild Theme Basicsadd_action()get_template_part()Shortcode with attributeSidebarsSecurity in WordPress - SanitizationinitCreate Template for Custom Post TypeFunction: add_action()Add ShortcodeHow Can I integrate Markdown editor with Advance Custom Field's repeater Add-on.Installation and Configurationwp_get_current_user()WP-CronSecure your installationOptions APIFunction : wp_trim_words()WP_Query() LoopUpdate WordPress ManuallyThemesWP-CLIDebuggingadd_menu_page()add_submenu_page()get_option()get_permalink()get_the_category()the_title()get_the_title()add_editor_style()add_theme_support()WordPress Plugin creationRun WordPress local with XAMPPAdmin Dashboard WidgetsSite MigrationMeta BoxRemove Auto Line Breaks From Content and Excerptget_home_path()Wordpress theme and child-theme developmentREST API

The $wpdb Object

Other topics

Remarks:

There are two ways to reference the $wpdb object. The first is to use the PHP keyword global in order to act on the global instance of the object.

global $wpdb;
echo $wpdb->prefix;
// Outputs the prefix for the database

The second way to use the $wpdb object is to reference PHP's $GLOBALS super global variable.

echo $GLOBALS['wpdb']->prefix;
// This will also output the prefix for the database

The second way is discouraged as it may not be considered the best practice.

Selecting a variable

In the most basic form, it is possible to select a single variable from a table by calling the object's get_var method passing in an SQL query.

global $wpdb;
$user = $wpdb->get_var( "SELECT ID FROM $wpdb->users WHERE user_email='[email protected]'" );

It is very important to note that any untrusted values used in queries must be escaped in order to protect against attacks. This can be done using the object's prepare method.

global $wpdb;
$email = $_POST['email'];
$user = $wpdb->get_var(
    $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_email=%s", $email )
);
if( !is_null( $user ){
    echo $user; 
} else {
    echo 'User not found';
}

Selecting multiple rows

You can use get_results to get multiple rows from database.

global $wpdb;
$userTable =$wpdb->prefix."users";

$selectUser = $wpdb->get_results("SELECT * FROM $userTable"); 

This will show all users list in array.

Contributors

Topic Id: 2691

Example Ids: 9014,31615

This site is not affiliated with any of the contributors.