## php – MySQL Query Returns Array () In Shortcode

Question

This is in my theme’s functions.php. I have an external MySQL database that I’m connecting to in WordPress with the following code, and I’m attempting to create a shortcode that shows the total of a column named total_downloads, with rows filtered by another column utm_campaign. The table name is reports_downloads. So, line 3 of my code returns the string “apple” and I want to get the sum of total_downloads for apple.

The problem is that this outputs Array ( ) on the page instead of a readable number. I’m struggling with PHP and more so with DB queries, but I’m trying to learn. Any guidance on how to get this function to return the integer 2?

function getAllDownloads() {
$wpdb2 = new WPDB( 'db_user', 'db_pass', 'db_name', 'db_host');$utm = do_shortcode('[wpmem_field utm_campaign]');
$result =$wpdb2->get_results("SELECT SUM(total_downloads) as result_value FROM reports_downloads WHERE utm_campaign = ".$utm.""); print_r($result);