Is there a (better) way to access $wpdb results?

This answer explains what the OP saw with column names and how to work with that, but the real answer is to use get_var() as in Howdy_McGee’s answer.


The string you’re seeing is the column name that MySQL is using for the result, because it doesn’t have any better ideas. One way is to give it an explicit name to use instead with AS, e.g.

global $wpdb;
$wpdbp = $wpdb->prepare('SELECT EXISTS ([some query] WHERE user_id =%d) AS `exists`;',
                        $target_user_id);
$result = $wpdb->get_results($wpdbp);

then the column name will be exists, i.e.

$result = $result[0]['exists'];

However I’m surprised there isn’t a ‘execute query and return scalar’ method in $wpdb that you can use instead to just fetch a single result like this. There is a better way, but I’d missed it as I was searching for terms like ‘scalar’, bah.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)