We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,949 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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.

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);
}
3
Contributors
4
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
5
Views
jonatec
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Have you tried json_encode()? http://php.net/manual/en/function.json-encode.php
But I'm afraid you have to loop anyway.

cereal
Veteran Poster
1,144 posts since Aug 2007
Reputation Points: 344
Solved Threads: 222
Skill Endorsements: 22

As cereal says you will need to use json_encode and you'll need to loop.

Looking at your code, Line 13 may make it difficult to parse in JSON.

$buf .= $id . '^' . $dept_name . '|';

I would use this instead:

$buf[] = array('id' => $id, 'dept_name' => $dept_name);

Then once I have all my results in the array I would use

echo json_encode($buf);
phper
Posting Whiz in Training
213 posts since Nov 2006
Reputation Points: 22
Solved Threads: 19
Skill Endorsements: 0

As cereal says you will need to use json_encode and you'll need to loop.

Looking at your code, Line 13 may make it difficult to parse in JSON.

$buf .= $id . '^' . $dept_name . '|';

I would use this instead:

$buf[] = array('id' => $id, 'dept_name' => $dept_name);

Then once I have all my results in the array I would use

echo json_encode($buf);

Excellent, thanks for the advice about using an array, but:
a) Is there a way to inspect the json before it gets sent?
b) Using your example and at the jQuery client-end, should I refer by id, that is: id and dept_name to pull out the values in the returned json?

Cheers.

jonatec
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

As cereal says you will need to use json_encode and you'll need to loop.

Looking at your code, Line 13 may make it difficult to parse in JSON.

$buf .= $id . '^' . $dept_name . '|';

I would use this instead:

$buf[] = array('id' => $id, 'dept_name' => $dept_name);

Then once I have all my results in the array I would use

echo json_encode($buf);

Just cracked it! Thanks, the method to use an array was useful and I used:
print_r(json_encode($array));
to inspect the json.

Cheers !

jonatec
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0748 seconds using 2.76MB