Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.