0

我希望有人可以帮助解决这个问题。

我有这个工作正常,它突然无缘无故地停止了。我得到一个表没有列错误。

我最初是从这个网站获得的代码 - http://mireille.it/example-code-realtime-google-chart-with-mysql-json-ajax/。不确定这是否有帮助。

这是我的代码:

标题

    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">
        // Load the Visualization API and the piechart package.
        google.load('visualization', '1', {'packages':['corechart']});

        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(drawChart);

        function drawChart() {
            var json = $.ajax({
                url: 'http://www.domain.com', // make this url point to the data file
                dataType: 'json',
                async: false
            }).responseText;

            // Create our data table out of JSON data loaded from server.
            var data = new google.visualization.DataTable(json);
            var options = {
                title: 'Active M&J Players by Team Assignment',
                is3D: 'true',
                width: 800,
                height: 600
            };
            // Instantiate and draw our chart, passing in some options.
            //do not forget to check ur div ID
            var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
            chart.draw(data, options);

            //setInterval(drawChart, 500 );
        }
    </script> 

PHP

    <?php
/* $server = the IP address or network name of the server
 * $userName = the user to log into the database with
 * $password = the database account password
 * $databaseName = the name of the database to pull data from
 * table structure - colum1 is cas: has text/description - column2 is data has the value
 */
$con = mysql_connect('database', 'username', 'password') or die('Error connecting to server');

mysql_select_db('database', $con); 

// write your SQL query here (you may use parameters from $_GET or $_POST if you need them)
$query = mysql_query('SELECT agelastsept as ageorder,CONCAT("U", agelastsept + 1 , "\'s") as agelastsept,total FROM members_family_view ORDER BY ageorder ASC');

$table = array();
$table['cols'] = array(
    /* define your DataTable columns here
     * each column gets its own array
     * syntax of the arrays is:
     * label => column label
     * type => data type of column (string, number, date, datetime, boolean)
     */
    // I assumed your first column is a "string" type
    // and your second column is a "number" type
    // but you can change them if they are not
    array('label' => 'agelastsept', 'type' => 'string'),
    array('label' => 'total', 'type' => 'number')
);

$rows = array();
while($r = mysql_fetch_assoc($query)) {
    $temp = array();
    // each column needs to have data inserted via the $temp array
    $temp[] = array('v' => $r['agelastsept']);
    $temp[] = array('v' => (int) $r['total']); // typecast all numbers to the appropriate type (int or float) as needed - otherwise they are input as strings

    // insert the temp array into $rows
    $rows[] = array('c' => $temp);
}

// populate the table with rows of data
$table['rows'] = $rows;

// encode the table as JSON
$jsonTable = json_encode($table);

// set up header; first two prevent IE from caching queries
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

// return the JSON data
echo $jsonTable;
?>

我已经完成了明显的检查并检查了 SQL 查询,并且工作正常。继续获取表格没有列错误。

谢谢,

约翰

4

0 回答 0