Skip to content

Instantly share code, notes, and snippets.

@AbhishekGhosh
Created August 19, 2014 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbhishekGhosh/df0201285a41aa5adbd8 to your computer and use it in GitHub Desktop.
Save AbhishekGhosh/df0201285a41aa5adbd8 to your computer and use it in GitHub Desktop.
Fetch Graph Data as JSON From MySQL
<?php
$link = mysql_connect( 'database_host', 'database_user', 'database_password' );
if ( !$link ) {
die( 'Could not connect: ' . mysql_error() );
}
$db = mysql_select_db( 'test', $link );
if ( !$db ) {
die ( 'Error selecting database \'test\' : ' . mysql_error() );
}
$query = "
SELECT *
FROM test_chart_data
ORDER BY category ASC";
$result = mysql_query( $query );
if ( !$result ) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die( $message );
}
$prefix = '';
echo "[\n";
while ( $row = mysql_fetch_assoc( $result ) ) {
echo $prefix . " {\n";
echo ' "category": "' . $row['category'] . '",' . "\n";
echo ' "value1": ' . $row['value1'] . ',' . "\n";
echo ' "value2": ' . $row['value2'] . '' . "\n";
echo " }";
$prefix = ",\n";
}
echo "\n]";
mysql_close($link);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment