简体   繁体   English

使用MySQL数据和PHP获取的Google图表?

[英]Google charts using MySQL data fetched with PHP?

I want to create a dynamic bar chart using Google's chart API and data obtained via MySQL. 我想使用Google的图表API和通过MySQL获取的数据来创建动态条形图。 I'm using PHP to create the pages. 我正在使用PHP创建页面。 I was able to create a simple chart with hard-coded values without any problem. 我能够用硬编码的值创建一个简单的图表,而没有任何问题。 Now I am trying to use some MySQL data instead, but with no luck. 现在我正尝试使用一些MySQL数据,但是没有运气。 This code generate an empty screen. 此代码生成一个空白屏幕。

       <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() {

          // Create our data table out of JSON data loaded from server.
          var data = new google.visualization.DataTable(<?=$jsonTable1?>);
          var options = {
               title: '',
              is3D: 'true',
              width: 230,
              height:145,
              animation:{
            duration: 100,
            easing: 'out',
          }
            };
          // Instantiate and draw our chart, passing in some options.
          // Do not forget to check your div ID
         var chart = new google.visualization.ColumnChart(document.getElementById('chart_div1'));
          chart.draw(data, options);
        }
        </script>
<html>
<body>
    <form method="post" action="">
      <input type="text" name="regi" >
      <br />
      <input type="submit" name="submit" >
      </form>

    <div id="chart_div1"></div>


  //php code

    <?php

    $DB_NAME = 'add';
    $DB_HOST = 'localhost';
    $DB_USER = 'root';
    $DB_PASS = '';

      $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

      if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
      }

    if(isset($_POST['submit']))
    {
    $regi=$_POST['regi'];
      $sth = $mysqli->query('SELECT * FROM overall WHERE year="$regi"');



      $rows = array();
      $table = array();
      $table['cols'] = array(

       array('label' => 'year', 'type' => "string"),

        array('label' => 'Final CSE-1', 'type' => 'number')




    );
    foreach($sth as $r) {

          $temp = array();
          $temp[] = array('v' => (string) $r['subjectcode']); 

    // Values of each slice
          $temp[] = array('v' => (int) $r['percentage']); 
          $rows[] = array('c' => $temp);

    }

    $table['rows'] = $rows;
    $jsonTable1 = json_encode($table);
    //echo $jsonTable;
    }
    ?>
</body>
</html>

If you using chrome, hit f12 and check console errors. 如果您使用的是chrome,请按f12键并检查控制台错误。 This will give us more info about that what failed. 这将为我们提供有关失败原因的更多信息。 Because you might have some broken values in database, or something like that. 因为您可能在数据库中有一些损坏的值,或类似的东西。 Because outside of that, it looks ok. 因为除此之外,它看起来还不错。

Also remember about SQL Injection . 还记得有关SQL Injection的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM