2017-11-25 181 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

是否有这个招数非常感谢你:) –