Interact with your WordPress Database

enter image description here

In WordPress, there is a global object called $wpdb which is used to interact with your WordPress database. $wpdb also stores the correct names for all the WordPress tables. To call the table names, you can use these variables:

$wpdb->posts; // Returns the name of of the posts table.
$wpdb->postmeta; // Returns the name of the post meta table.
$wpdb->commentmeta; // Returns the name of the comment meta table.
$wpdb->comments; // Returns the name of the comments table.
$wpdb->terms; // Returns the name of the terms table.
$wpdb->term_relationships; // Returns the name of the term relationships table.
$wpdb->term_taxonomy; // Returns the name of the term taxonomy table.
$wpdb->links; // Returns the name of the links table.
$wpdb->options; // Returns the name of the options table.
$wpdb->usermeta; // Returns the name of the user meta table.
$wpdb->users; // Returns the name of the users table.

For example, in this example custom_form_submission function, instead of hard-coding 'wphk_posts', you should use $wpdb->posts to get the correct posts table name. The corrected SQL query would like this:

$results = $wpdb->get_results($wpdb->prepare(
            "SELECT post_content FROM {$wpdb->posts} WHERE post_content LIKE %s AND post_content LIKE %s AND post_status = 'publish'",
            '%' . $wpdb->esc_like($payer_email) . '%',
            '%' . $wpdb->esc_like($product_name) . '%'
        ), ARRAY_A);

This way WordPress takes care of any table prefix (ie. "wp_" or "wphk_" etc.) that might be different from server to server.