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?

utm_campaign total_downloads
apple 1
orange 1
apple 1
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);
    echo $result[0]->result_value;
}
add_shortcode('b3_all_downloads', 'getAllDownloads');

0
Adam ImaginedAtom Mitchell 3 months 2022-06-22T15:24:56-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse