line chart from mysql example

629 views
Skip to first unread message

dror h

unread,
Aug 27, 2011, 3:28:00 PM8/27/11
to Google Visualization API
Hello,

I am a first time user and am trying to create a line chart from my
database, here is the code i have so far
but it doesnt seem to work, any help would be much
appreciated...THANKS!

<?php


$dbhost =
$dbusername =
$dbpasswd =
$database_name =


$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
or die ("Couldn't connect to server.");
mysql_select_db(datebase_name) or die('Could not select database');


$sql = "SELECT count(id) AS count, DATE_FORMAT(date, '%Y, %m-1, %d')
AS date FROM date WHERE weight_date NOT LIKE '0000-00-00 00:00:00' AND
date
NOT LIKE '2000-00-00 00:00:00' GROUP BY DATE_FORMAT(date, '%Y, %m,
%d')";
$sql = mysql_query($sql);
$rownum = mysql_num_rows($sql);


?>
<html>
<head>
<script type="text/javascript" src="http://www.google.com/
jsapi"></
script>
<script type="text/javascript">
google.load("visualization", "1", {packages:
["Linechart"]});
google.setOnLoadCallback(drawData);
function drawData() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('worlds', 'crayons);
<?php
echo " data.addRows($rownum);\n";
while($row = mysql_fetch_assoc($sql)) {


$int_y_pos += $int_y_step_small;


echo " data.setValue(" . $int_y_pos . ", 0, new Date(" .
$row['date'] . "));\n";
echo " data.setValue(" . $int_y_pos . ", 1," .
$row['count'] . ");
\n";



}


?>
var time = new
google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240, title:
'Performance'}); }
time.draw(data, {displayExactValues:true});

}
</script>
</head>


<body>
<div id="time_div" style="width: 940px; height: 240px;"></div>
</body>
</html>

Bruce Smith

unread,
Aug 27, 2011, 5:34:56 PM8/27/11
to google-visua...@googlegroups.com
Just from a quick glance, it looks like 'worlds' in your 2nd addColumn declaration is not a valid data type.

Is it generating page source? Can you post that?
 

Bruce Smith



From: dror h <dror...@gmail.com>
To: Google Visualization API <google-visua...@googlegroups.com>
Sent: Saturday, August 27, 2011 3:28 PM
Subject: [visualization-api] line chart from mysql example
--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualization-api+unsub...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.



dror h

unread,
Aug 28, 2011, 12:22:03 AM8/28/11
to Google Visualization API
I'm really not sure why this isn't working...
This is the page source:


<br />
<b>Warning</b>: mysql_num_rows(): supplied argument is not a valid
MySQL result resource in <b>D:\Hosting\5342847\html\stptwo.php</b> on
line <b>21</b><br />

<html>
<head>
<script type="text/javascript" src="http://www.google.com/
jsapi"></
script>
<script type="text/javascript">
google.load("visualization", "1", {packages:
["Linechart"]});
google.setOnLoadCallback(drawData);
function drawData() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('worlds', 'crayons);
data.addRows();
<br />
<b>Warning</b>: mysql_fetch_assoc(): supplied argument is not a valid
MySQL result resource in <b>D:\Hosting\6192836\html\steptwo.php</b> on
line <b>40</b><br />

Bruce Smith

unread,
Aug 28, 2011, 12:05:49 PM8/28/11
to google-visua...@googlegroups.com
I suggest you include your connection as an argument to your mysql_query call...

$sql_result = mysql_query($sql,$connection)

Also, reusing the $sql variable as your result set after defining it as the query string is a bit confusing. It might not actually be causing problems, but I would suggest using a different variable name to avoid confusion
 
Bruce


From: dror h <dror...@gmail.com>
To: Google Visualization API <google-visua...@googlegroups.com>
Sent: Sunday, August 28, 2011 12:22 AM
Subject: [visualization-api] Re: line chart from mysql example

dror h

unread,
Aug 28, 2011, 1:49:40 PM8/28/11
to Google Visualization API
I've tried that as well but it doesnt seem to work...
do you perhaps have an example script using php to get mysql data??

Bruce Smith

unread,
Aug 28, 2011, 10:43:39 PM8/28/11
to google-visua...@googlegroups.com
I've compared your code to mine and can't see anything you're doing wrong. I did notice when you issue mysql_select_db in your original code you didn't include the $ in the argument... mysql_select_db(database_name) rather than mysql_select_db($database_name)... but I would expect that to be trapped and your script die (which apparently it doesn't). It might be worth double-checking that.
 
Bruce


From: dror h <dror...@gmail.com>
To: Google Visualization API <google-visua...@googlegroups.com>
Sent: Sunday, August 28, 2011 1:49 PM

Subject: [visualization-api] Re: line chart from mysql example

I've tried that as well but it doesnt seem to work...
do you perhaps have an example script using php to get mysql data??

asgallant

unread,
Aug 29, 2011, 10:06:45 AM8/29/11
to google-visua...@googlegroups.com, Bruce Smith
Since you are getting an error related to the mysql_num_rows() function, it seems likely that the problem is either in your SQL itself or the code you are using to connect to the database.  Try using a simple query (like "SELECT * from date") and see if that returns anything.  If not, then the problem is probably in your connection code (the missing "$" in front of "database_name" seems a probable cause, but as BruceS mentioned, I would think that would trip the "or die" clause).  If the simple select works, then try your select statement from the MySQL command line or your database interface (something like PHPMyAdmin) and see what it returns.

Bruce Smith

unread,
Aug 29, 2011, 10:42:45 AM8/29/11
to google-visua...@googlegroups.com
Another thought is to trap for errors in the mysql_query statement, since whatever it's  generating isn't being interpreted as an acceptable result set by the server.
 

Bruce Smith



From: asgallant <drew_g...@abtassoc.com>
To: google-visua...@googlegroups.com
Cc: Bruce Smith <bruce_...@yahoo.com>
Sent: Monday, August 29, 2011 10:06 AM
Subject: Re: [visualization-api] Re: line chart from mysql example
Reply all
Reply to author
Forward
0 new messages