0

I am trying to convert a mySQL query into a JSON object. This works:

<?php
// load in mysql server configuration (connection string, user/pw, etc)
include 'mysqlConfig.php';
$year = $_GET['year'];
// connect to the database
@mysql_select_db($dsn) or die( "Unable to select database");

// outputs the db as lines of text.
$result = mysql_query("SELECT COUNTY, COUNT(TYPE) AS 'type'  from data WHERE DATE between '2000-01-01' and '2000-12-31' GROUP BY COUNTY");
$rows = array();

while($r = mysql_fetch_assoc($result)) {
     $rows[] = $r;
}

echo json_encode($rows);
mysql_close();

?>

But I get this output:

[{"COUNTY":"Carlow","type":"121"},{"COUNTY":"Cavan","type":"130"},{"COUNTY":"Clare","type":"112"},{"COUNTY":"Cork","type":"833"},{"COUNTY":"Donegal","type":"264"},{"COUNTY":"Dublin","type":"2457"},{"COUNTY":"Galway","type":"287"},{"COUNTY":"Kerry","type":"227"},{"COUNTY":"Kildare","type":"300"},{"COUNTY":"Kilkenny","type":"139"},{"COUNTY":"Laois","type":"123"},{"COUNTY":"Leitrim","type":"39"},{"COUNTY":"Limerick","type":"370"},{"COUNTY":"Longford","type":"85"},{"COUNTY":"Louth","type":"257"},{"COUNTY":"Mayo","type":"231"},{"COUNTY":"Meath","type":"268"},{"COUNTY":"Monaghan","type":"136"},{"COUNTY":"Offaly","type":"97"},{"COUNTY":"Roscommon","type":"115"},{"COUNTY":"Sligo","type":"113"},{"COUNTY":"Tipperary","type":"249"},{"COUNTY":"Waterford","type":"205"},{"COUNTY":"Westmeath","type":"118"},{"COUNTY":"Wexford","type":"246"},{"COUNTY":"Wicklow","type":"235"}]

whereas this is the format I am looking for:

{"Carlow":3,"Cavan":4,"Clare":5,"Cork":3,"Donegal":4,"Dublin":5,"Galway":4,"Kerry":5,"Kildare":5,"Kilkenny":12,"Laois":4,"Leitrim":4,"Limerick":4,"Longford":4,"Louth":4,"Mayo":5,"Meath":3,"Monaghan":5,"Offaly":4,"Roscommon":3,"Sligo":3,"Tipperary":4,"Waterford":2,"Westmeath":2,"Wexford":4,"Wicklow":2}

Any ideas?

1
  • Please explain in detail. What does 3,4,5 stands for?? Commented Apr 6, 2012 at 15:21

2 Answers 2

2

It should be:

while($r = mysql_fetch_assoc($result)) {
     $rows[$r['COUNTRY']] = $r['type'];
}
Sign up to request clarification or add additional context in comments.

4 Comments

i guess eoin is not looking to accept $r['type']. May be he wants to add 3,4,5,etc. not 121,130,etc.(i.e. type), if i am not wrong.
Hey that's brilliant, thanks a million. One more thing, is there anyway to easily make the value into an integer i.e. to get rid of the double quotes around the values. I am using this script to pass the sql values into the javascript based d3 framework.
@eoin just use type cast (int)$r['type'];
thanks xdazz, i actually just got it with intval($r['type']). I've been going around in circles for hours. Thanks so much.
0
while($r = mysql_fetch_assoc($result)) {
 $rows[] = $r;
}

Should be

while($r = mysql_fetch_assoc($result)) {
 $rows[] = array($r['COUNTY'], $r['type']);
}

2 Comments

that brings me closer alright but not quite what I need:[["Carlow","121"],["Cavan","130"],["Clare","112"],["Cork","833"],["Donegal","264"],["Dublin","2457"],["Galway","287"],["Kerry","227"],["Kildare","300"],["Kilkenny","139"],["Laois","123"],["Leitrim","39"],["Limerick","370"],["Longford","85"],["Louth","257"],["Mayo","231"],["Meath","268"],["Monaghan","136"],["Offaly","97"],["Roscommon","115"],["Sligo","113"],["Tipperary","249"],["Waterford","205"],["Westmeath","118"],["Wexford","246"],["Wicklow","235"]]
Is it possible to get rid of the sqaure brackets, have everything between 1 pair of curly brackets and have double quotes around the county not the the value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.