2017-11-25 91 views
0

我有以下Google Charts代碼,我試圖將它與MySQL連接起來,我已經嘗試過所有可用的互聯網,但它仍然只顯示一個SQL結果集條目,儘管我有10個條目。在互聯網上我發現了$row_num反事物,並且因爲將它包含在我的代碼中,我已經能夠查詢一個條目,但我想用它查詢所有條目。請幫我看看這個,讓我知道我一直在做什麼錯誤(如果有的話),以下是我的PHP代碼:谷歌圖表連接時只顯示MySQL的一個條目?

[<?php 

``$dbhandle= new mysqli('localhost','root','8317','record'); 
//echo $dbhandle->connect_error(); 

$query= "SELECT * from download_manager"; 
$res= $dbhandle->query($query); 
?> 

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

     function drawStuff() { 
     var data = new google.visualization.arrayToDataTable(\[ 
      \['Title', 'No. of Downloads'\], 
      <?php 
      $total_rows = mysqli_num_rows($res); 
      $row_num = 0; 
if (mysqli_num_rows($res) > 0) { 
while($row=$res->fetch_assoc()) 
{ 

    $row_num++; 

    if ($row_num == $total_rows){ 
    echo "\['".$row\['filename'\]."',".$row\['downloads'\]."\]"; 
} 

} 
} 
else 
{ 
    echo "0 results"; 
} 
      ?> 
     \]); 

     var options = { 
      width: 800, 
      chart: { 
      'title': 'Top 10 Downloads', 
      subtitle: 'for the last month' 
      }, 
      bars: 'horizontal', // Required for Material Bar Charts. 
      axes: { 
      } 
     }; 

     var chart = new google.charts.Bar(document.getElementById('dual_x_div')); 
     chart.draw(data, options); 
    }; 
    </script> 
    </head> 
    <body> 
    See Also : <a href="barchart1.html"> Downloads in the Last 7 Years </a> <br> <br> 

    <div id="dual_x_div" style="width: 900px; height: 500px;"></div> 
    </body> 
</html>][1] 

回答

0

你已經錯過了加逗號,在你的PHP數據環路。嘗試使用以下內容:

echo "\['".$row\['filename'\]."',".$row\['downloads'\]."\],"; 
+0

是否有這個招數非常感謝你:) –