PHP returning JSON

Hi

Please take a look at the code section. Does anyone know how to:
a) Return JSON from the prepared procedure call instead of me using a while loop and a concatenated string?
b) Given the MySQL query returns these columns: ID, DepartmentName how do I access these in my client-side JavaScript, ie what will the JSON look like ?

Many thanks.

[php]
function fnGetDepartments() {

require ('mysqli_connect.php'); // Connect to the Db.	
$sql = "CALL get_departments(?)";
$stmt = $dbc->prepare($sql);
if ($dbc->errno) {die($dbc->errno.":: ".$dbc->error);}
$stmt->bind_param("i", $prm);
$stmt->execute( );
if ($dbc->errno) {die($dbc->errno.": ".$dbc->error);}
$stmt->bind_result($id, $dept_name);
$buf = "";
while ($stmt->fetch( )) {
	$buf .= $id . '^' . $dept_name . '|';
}
$buf = substr($buf, 0, strlen($buf)-1);
echo $buf;	
mysqli_close($dbc);

}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service