如何使用jsapi显示图形

时间:2013-11-20 01:58:03

标签: graph

我使用php,我想创建图形(线图)。这是我的代码:

  <html>
<?php

mysql_connect("localhost","root","");
mysql_select_db("sperformance"); 
$query = mysql_query("SELECT * FROM stud_details WHERE email='nazirah.mnazir@gmail.com';");
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
while($row = mysql_fetch_row($result)) {
?>

 <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
    ['x',           'CGPA'     ],
    ['Semester 1',  <?php echo $row[1] ?>     ],
    ['Semester 2',  <?php echo $row[2] ?>     ],
    ['Semester 3',  <?php echo $row[3] ?>     ],
    ['Semester 4',  <?php echo $row[4] ?>     ],
    ['Semester 5',  <?php echo $row[5] ?>     ],
    ['Semester 6',  <?php echo $row[6] ?>     ],
    ['Semester 7',  <?php echo $row[7] ?>     ],
    ['Semester 8',  <?php echo $row[8] ?>     ],
    ['Semester 9',  <?php echo $row[9] ?>     ],
    ['Semester 10', <?php echo $row[10] ?>    ],
    ['Semester 11', <?php echo $row[11] ?>    ],
    ['Semester 12', <?php echo $row[12] ?>    ],

  ]);
        var options = {
          title: 'Student Performance'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 1300px; height: 500px;"></div>
  </body>
  <?php
  }
  ?>
</html>

当我跑步时,它显示第7行的错误, 警告:mysql_query()期望参数1为字符串。有人可以帮帮我吗?谢谢。

1 个答案:

答案 0 :(得分:1)

嗯...你为什么要调用两次相同的函数mysql_query? 请尝试替换以下行:

$query="SELECT * FROM stud_details WHERE email='nazirah.mnazir@gmail.com'";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

我宁愿选择准备好的语句(使用MySQLi或PDO),或者至少在使用它们之前真正转义值,例如:

$email='nazirah.mnazir@gmail.com';// or whatever so this one can have different values.
$query = sprintf("SELECT * FROM stud_details WHERE email='%s'",mysql_real_escape_string($email));
$result = mysql_query($query);

希望它适合你。