The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Unable to display google charts
jack83
post Mar 1 2018, 03:49 AM
Post #1





Group: Members
Posts: 5
Joined: 1-March 18
Member No.: 26,601



Hi, I am trying to create a webpage which shows google charts where I will get real time data from mysql database. I put the .php file into htdocs in xampp to view the webpage. However, the webpage is blank. There are no errors. Below is my code, would appreciate some help here.


<html>
<head>
<meta http-equiv="refresh" content="10">
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);

function drawChart () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Time');
data.addColumn('number', 'Power');
data.addRows([
<?php

$db="fyp";
$link = mysql_connect("localhost", "root", "password");
mysql_query('SET NAMES utf8');
mysql_select_db($db , $link) or die ("Couldn't open $db: ".mysql_error ());
$result = mysql_query("SELECT TIMESTAMPDIFF (MINUTE, Time, now ()) as Timel, Power FROM temp WHERE TIMESTAMPDIFF (MINUTE, Time, now()) < 60 Order By Time");

if ($result !== false) {
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {

$Time=mysql_result ($result, $i, "Timel");
$Power=mysql_result ($result, $i, "Power");

echo "['";
echo "$Time";
echo "',";
echo "$Power";
echo "]";
if ($i < ($num - 1))
{
echo ",";
}
$i++;
}
}
?>
]);

var options = {
width: 1000, height: 300,
hAxis: {title: 'Minute ago'},
vAxis: {title: 'Power(W)', maxValue: 3000, minValue: 0}
};

var chart = new google.visualization.ColumnChart (document.getElementById("chartl"));
chart.draw (data, options);
}

function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn (string', 'Time);
data.addColumn('number', 'Energy');
data.addRows ([

<?php

$db="fyp";
$link = mysql_connect("localhost", "root", "password");
mysql_query('SET NAMES utf8');
mysql_select_db($db , $link) or die ("Couldn't open $db: ".mysql_error ());
$result = mysql_query("SELECT now() as Timel, sum(Power) as Energy FROM temp WHERE TIMESTAMPDIFF (MINUTE, Time, now()) < 60 Order By Time");
if ($result !== false) {
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {

$Time=mysql_result ($result, $i, "Timel");
$Energy=mysql_result ($result, $i, "Energy") / 1000;

echo "['";
echo "$Time";
echo "',";
echo "$Energy";
echo"]";
if ($i < ($num - 1))
{
echo ",";
}
$i++;
}
}
?>
]);

var options = {
width: 1000, height: 300,
hAxis: {title: 'Current Time'},
vAxis: {title: 'Energy(kWh)', maxValue: 10, minValue: 0}
};

var chart = new google.visualization.ColumnChart (document.getElementById("chart2"));
chart.draw (data, options);
}

</head>
<body>
<div align="center">
<H1 align="center">Real-Time Energy Consumption Chart</H1>
<div align="center">
<table width="900" border="1">
<tr>
<td><div align="center">
<table width="1000" border="0">
<tr>
<td width="141">&nbsp;</td>
<td width="10">&nbsp;</td>
<td width="432"><div id="chart1"></div></td>
<td width="141">&nbsp;</td>
<td width="144">&nbsp;</td>
</tr>
<tr>
<td width="141">&nbsp;</td>
<td width="10">&nbsp;</td>
<td width="432"><div id="chart2"></div></td>
<td width="141">&nbsp;</td>
<td width="144">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><div align="center">

<?php

$db="fyp";
$link=mysql_connect ("localhost", "root", "password");
mysql_query('SET NAMES utf8');
mysql_select_db($db, $link) or die ("Couldn't open $db: ".mysql_error());
$result = mysqli_query ("SELECT Time, Power FROM temp Order By time desc limit 1");

if ($result !== false) {
$num=mysql_numrows($result);
$Time=mysql_result($result, 0, "Time");
$Power=mysql_result($result, 0, "Power");
}

?>

</div></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</div></td>
</tr>
</table>
</div>
</body>
</html>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 1 2018, 12:51 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,549
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



I think the page is blank because of a PHP error. Your php error log should have some information about it. Looking over your code I see you are using mysql_* functions. I don't know what PHP version you are using but all mysql_* functions have been removed starting from version 7.

You should be using mysqli_* functions or PDO.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 16th November 2018 - 01:43 PM