2013-07-02 49 views
0

我試圖通過關於獲得從MySQL JSON字符串,並準備好應對highcharts教程的工作,但它不工作,我不知道爲什麼....我的服務器配置可以停止json的工作嗎?

HTML:

<script type="text/javascript" src="js/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(function() { 
     var chart; 
     $(document).ready(function() { 
      $.getJSON("data.php", function (json) { 

       chart = new Highcharts.Chart({ 
        chart: { 
         renderTo: 'container', 
         type: 'line', 
         marginRight: 130, 
         marginBottom: 25 
        }, 
        title: { 
         text: 'Revenue vs. Overhead', 
         x: -20 //center 
        }, 
        subtitle: { 
         text: '', 
         x: -20 
        }, 
        xAxis: { 
         categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 
        }, 
        yAxis: { 
         title: { 
          text: 'Amount' 
         }, 
         plotLines: [ 
          { 
           value: 0, 
           width: 1, 
           color: '#808080' 
          } 
         ] 
        }, 
        tooltip: { 
         formatter: function() { 
          return '<b>' + this.series.name + '</b><br/>' + 
           this.x + ': ' + this.y; 
         } 
        }, 
        legend: { 
         layout: 'vertical', 
         align: 'right', 
         verticalAlign: 'top', 
         x: -10, 
         y: 100, 
         borderWidth: 0 
        }, 
        series: json 
       }); 
      }); 

     }); 

    }); 
</script> 
</head> 
<body> 
<script src="js/highcharts.js"></script> 
<script src="js/exporting.js"></script> 

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> 

<?php 
$con = mysql_connect("*****", "*****", "*****"); 

if (!$con) { 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("*****", $con); 

$sth = mysql_query("SELECT revenue FROM projections_sample"); 
$rows = array(); 
$rows['name'] = 'Revenue'; 
while ($r = mysql_fetch_array($sth)) { 
    $rows['data'][] = $r['revenue']; 
} 

$sth = mysql_query("SELECT overhead FROM projections_sample"); 
$rows1 = array(); 
$rows1['name'] = 'Overhead'; 
while ($rr = mysql_fetch_assoc($sth)) { 
    $rows1['data'][] = $rr['overhead']; 
} 

$result = array(); 
array_push($result, $rows); 
array_push($result, $rows1); 

print json_encode($result, JSON_NUMERIC_CHECK); 

mysql_close($con); 
?> 

JSON字符串正在使用該文件

data.php產生210

如果我手動輸入JSON字符串到data.php圖表按預期方式顯示。

[{ 
"name": "Revenue", 
"data": [23987, 24784, 25899, 25569, 25897, 25668, 24114, 23899, 24987, 25111, 25899, 23221] 
}, { 
"name": "Overhead", 
"data": [21990, 22365, 21987, 22369, 22558, 22987, 23521, 23003, 22756, 23112, 22987, 22897] 
}] 

我假設我的連接信息是正確的,因爲如果我故意輸入錯誤密碼,我得到了MySQL連接錯誤......

「Could not connect: Access denied for user ‘*****’@'localhost’ (using password: YES)」 

而且,我得到「True」返回當我添加「echo @mysql_ping() ? ‘true’ : ‘false’;」到頁面的底部。

因爲我相信它應該工作我想知道是否有任何與服務器配置有關的事情可能會阻止我使用JSON?

+0

*必須:*本'mysql_ *'功能將被已廢棄在PHP 5.5](http://php.net/manual/en/faq.databases.php#faq.databases.mysql .deprecated)。不建議編寫新代碼,因爲它將來會被刪除。相反,無論是[MySQLi](http://php.net/manual/en/book.mysqli.php)還是[PDO](http://php.net/manual/en/book.pdo.php)和[是一個更好的PHP開發人員](http://jason.pureconcepts.net/2012/08/better-php-developer/)。 –

+0

請在執行查詢後檢查mysql錯誤:'$ result = mysql_query(「SELECT overhead FROM projections_sample」); if(!result)print mysql_error;'它是調試的第一步.. – immulatin

+1

打印json編碼數據時實際正在打印什麼?嘗試添加'alert(json)'到javascript,看看它是否返回有效的json! –

回答

0

,如果你不能使用JSON_NUMERIC_CHECK(因爲它需要PHP 5.3.3(json.constants))

可以castint [$number = (int)"1234";]

while ($r = mysql_fetch_array($sth)) { 
    $rows['data'][] = (int)$r['revenue']; // <- (int) 
} 

while ($rr = mysql_fetch_assoc($sth)) { 
    $rows1['data'][] = (int)$rr['overhead']; // <- (int) 
} 

print json_encode($result); 
+0

這真是太棒了,Iv'e花了好幾個小時試圖解決這個問題。從練習中學到了一些公平的東西。謝謝你不夠... :) – SG129

相關問題