Support » Plugins » Accessing MySQL Databases and Variables with WPDB

  • Website: (name placeholder for time being)

    I am using a plugin that has created an extra WP table within my MySQL database. That table’s name is dcq_wpmlm_users. Here is the code:

    global $current_user;
                        if ( is_user_logged_in() ) {
                            $wp_user_id = $current_user->ID;
                            $mlm_id_query = "SELECT id FROM $wpdb->dcq_wpmlm_users WHERE user_id = $wp_user_id";
                            $mlm_id_prepared = $wpdb->prepare( "SELECT id FROM dcq_wpmlm_users WHERE user_id = %d", $wp_user_id );
                            $mlm_prepared_result = $wpdb->get_var( $mlm_id_query );
                            $mlm_id_results = $wpdb->get_results( $mlm_id_query, OBJECT );
                            echo '<span id="mlm-user-id" data-mlm_id="' . $mlm_id_results->id . '" data-user_id="' . $wp_user_id . '">' . var_dump(get_object_vars($mlm_id_results)) . '</span>';
                            echo '<span id="mlm-user-id" data-mlm_id="' . $mlm_prepared_result . '" data-user_id="' . $wp_user_id . '">' . var_dump(get_object_vars($mlm_prepared_result)) . '</span>';

    You will notice the two lines toward the end are similar, that’s just because I’m trying different methods to print or echo out my results (or what I thought *should* be my results) but while the $wp_user_id (actual ID of the current user, from the wp_users table) prints fine, the other variable (a primary key from the dcq_wpmlm_users that I am using to verify if the user exists in the dcq_wpmlm_users table).

    It seems either I am doing the query wrong or handling the results wrong. Can someone give me a gentle nudge in the right direction? More than anything it’d be nice to see some examples of code that is similar to what I am attempting here. I specifically only need the id variable from the table, not necessarily the whole row of the result (of which there should only be one row). Thank you.

  • The topic ‘Accessing MySQL Databases and Variables with WPDB’ is closed to new replies.