Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2012, 05:03 PM   PM User | #1
jonatec
New Coder

 
Join Date: Aug 2008
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jonatec is an unknown quantity at this point
PHP return 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 Code:
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($buf0strlen($buf)-1);
    echo 
$buf;    
    
mysqli_close($dbc);

jonatec is offline   Reply With Quote
Old 02-21-2012, 10:55 PM   PM User | #2
cercos
New Coder

 
Join Date: Feb 2012
Posts: 39
Thanks: 0
Thanked 9 Times in 9 Posts
cercos is an unknown quantity at this point
well if $buf is the data you want to return to javascript it would be:

PHP Code:
echo  json_encode($buf); 

then to access it in javascript you would just reference it as the same variable name as the php script.
with jquery you could do something like:
Code:
$.post('/YOUR_PHP_SCRIPT.php', $('#YOUR_FORM').serialize(),
function(buf) {
	if(buf)
	{
		//reutrned true do something good
	}
	else
	{
		//returned false do somthing evil
	}
}, 'json');

Last edited by cercos; 02-21-2012 at 11:02 PM..
cercos is offline   Reply With Quote
Old 02-23-2012, 11:15 AM   PM User | #3
jonatec
New Coder

 
Join Date: Aug 2008
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jonatec is an unknown quantity at this point
Thanks for getting back..

But can I use json_encode directly with $stmt and not have to build up a string, kinda defeats the object ??

Cheers.
jonatec is offline   Reply With Quote
Old 02-23-2012, 03:40 PM   PM User | #4
jonatec
New Coder

 
Join Date: Aug 2008
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
jonatec is an unknown quantity at this point
Just cracked it! Thanks, the method to use an array was useful and I used:
print_r(json_encode($array));
to inspect the json.
jonatec is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:00 AM.


Advertisement
Log in to turn off these ads.