Google Charts With Data from Wamp Server Database

433 views
Skip to first unread message

Nelson Idachi

unread,
Nov 8, 2013, 3:57:48 PM11/8/13
to google-visua...@googlegroups.com
Hello..I am an android programmer and am doing a feedback system that requires me to send data to a server
The problem is that i dont know how to create Google charts in php with data from wamp server database, i have
tried following up with some example here but i could not understand.
So if i can get step by step help on how i can come up with those charts i will be greatful that is what i start with and what follows.
i only know how to use php. Thank you

asgallant

unread,
Nov 8, 2013, 4:12:25 PM11/8/13
to google-visua...@googlegroups.com
This thread has a ton of examples to work from.  There is nothing special about WAMP that would make a difference compared with a standard LAMP (Linux/Apache/MySQL/PHP) stack.

Nelson Idachi

unread,
Nov 8, 2013, 4:34:26 PM11/8/13
to google-visua...@googlegroups.com


I have viewed the post in the thread but i cant find how i can start off that is the files i have to create and the codes for each

asgallant

unread,
Nov 8, 2013, 4:55:40 PM11/8/13
to google-visua...@googlegroups.com
You have two basic choices to make: you can either load data in the page, or you can set up an AJAX system to dynamically fetch data from your server.  The first is slightly easier to implement, and the second is more flexible.

Nelson Idachi

unread,
Nov 8, 2013, 5:00:19 PM11/8/13
to google-visua...@googlegroups.com
i think i will go with the first option. Can you guide me through that?


asgallant

unread,
Nov 8, 2013, 6:44:08 PM11/8/13
to google-visua...@googlegroups.com
Ok, here's your basic structure:

<?php 
$username = 'mysql username';
$password = 'mysql password';
$databasename = 'mysql database name';

try {
$db = new PDO("mysql:dbname=$databasename", $username, $password);
}
catch (PDOException $e) {
die("{error: {$e->getMessage()}}");
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$query = $db->prepare("SELECT column1, column2, etc... FROM myTable");
try {
$query->execute();
$results = $query->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
die("{error: {$e->getMessage()}}");
}

$table = array(
'cols' => array(
array('label' => 'Column 1' , 'type' => '<column 1 data type>'),
array('label' => 'Column 2' , 'type' => '<column 2 data type>')
// etc
),
'rows' => array()
);

foreach ($results as $r) {
$table['rows'][] = array('c' => array(
array('v' => $r['column1']),
array('v' => $r['column2'])
// etc
));
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>My Chart</title>
<script type='text/javascript' src="http://www.google.com/jsapi"></script>
<script type='text/javascript'>
//<![CDATA[ 
function drawChart () {
var data = new google.visualization.DataTable(<?php echo json_encode($table, JSON_NUMERIC_CHECK); ?>);
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
chart.draw(data, {
height: 400,
width: 600
});
}
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});
//]]>  
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
Message has been deleted

Nelson Idachi

unread,
Nov 9, 2013, 7:16:35 AM11/9/13
to google-visua...@googlegroups.com
The problem is my database does not have a password and still its not working. Kindly check to see if there is anything i did not do right


<?php 
$username = 'localhost';
$password = '';
$databasename = 'mobiledb';

try {
$db = new PDO("mysql:dbname = $mobiledb", $localhost, $);
}
catch (PDOException $e) {
die("{error: {$e->getMessage()}}");
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$query = $db->prepare("SELECT Q1, Q2, FROM table2");
try {
$query->execute();
$results = $query->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
die("{error: {$e->getMessage()}}");
}

$table = array(
'cols' => array(
array('label' => 'Q1' , 'type' => '<varchar>'),
array('label' => 'Q2' , 'type' => '<varchar>')
// etc
),
'rows' => array()
);

foreach ($results as $r) {
$table['rows'][] = array('c' => array(
array('v' => $r['Q1']),
array('v' => $r['Q2'])

asgallant

unread,
Nov 9, 2013, 12:36:07 PM11/9/13
to google-visua...@googlegroups.com
Ok, a couple of things need to be fixed here:

First, you need to include the password in the PDO constructor, even if the password is blank, so this line must include the $password variable:

$db = new PDO("mysql:dbname = $mobiledb", $localhost, $password);

Then, for the columns, there are a few valid data types: "string", "number", "date", "datetime", "timeofday". "boolean".  If you are using "date", "datetime", or "timeofday", chances are that your database output will require some modifications before you can add them to the chart data, but if you are using the others, you can output them as-is.  Given that you chose "varchar" for the types, "string" is likely the most appropriate data type for your columns:

array('label' => 'Q1' , 'type' => 'string'),
array('label' => 'Q2' , 'type' => 'string')
Reply all
Reply to author
Forward
0 new messages