Domanda

I am trying to get this Query to work but something must be wrong with my query. I can pull all the data out but can't figure out how to pull out data that matches a specific username. This is built on wordpress with buddypress integration.

global $wpdb;
$ttm_username = bp_get_displayed_user_fullname();

$trackingData = $wpdb->get_results("SELECT * FROM `wp_shipping_numbers` WHERE `username` = $ttm_username ");
È stato utile?

Soluzione

If you are getting the username with bp_get_displayed_user_fullname() function, that returns you as a string value.

$wpdb->get_results(
    "SELECT * FROM `wp_shipping_numbers` WHERE `username` = $ttm_username "
);

Use

$wpdb->get_results(
    "SELECT * FROM `wp_shipping_numbers` WHERE `username` = '".$ttm_username."' "
);

Altri suggerimenti

You are placing $ttm_username in your string as text, and not a string. So that needs to be fixed. Also, to save headaches in formatting & debugging, I do the following: I like to use single quotes to delineate strings. And I like to break up queries into easier to digest “per line” chunks when setting in a string.

global $wpdb;
$ttm_username = bp_get_displayed_user_fullname();

$query = 'SELECT *'
       . ' FROM `wp_shipping_numbers`'
       . ' WHERE `username` = ' . $ttm_username
       ;
$trackingData = $wpdb->get_results($query);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top