I’m having hard time troubleshooting why i cannot seem to run this inside wordpress like on the index.php. It works when its standalone. I created additional table inside wordpress db. Im seeing blank data when i run. Somehow its not picking up the data. I came to know after research i need to use $wpdb and get_results. so I converted it from this old code :

$result = $db->query("SELECT name,rating FROM wp_figure where status = '1' ORDER BY rating DESC");

/to wordpress code/

global $wpdb;
$results = $wpdb->get_results("SELECT name,rating FROM wp_figure where status = '1' ORDER BY rating DESC");

//on the header.php

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});

function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['name', 'Rating'],
      if($result->num_rows > 0){
          while($row = $result->fetch_assoc()){
            echo "['".$row['name']."', ".$row['rating']."],";

    var options = {
        title: 'Figure',
        width: 900,
        height: 500,

    var chart = new google.visualization.PieChart(document.getElementById('piechart'));

    chart.draw(data, options);

on the body html i have

    <!-- Display the pie chart -->
    <div id="piechart"></div>

Read more here: WordPress sql fetch_assoc


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: