[wd_asp elements=’search’ ratio=’100%’ id=1]

JSON encode mysql result

23rd July 2013

MySql

mysql codehaven category
SEND SIDE (myfile.php)


RECEIVE SIDE (process.php)


//get values as you would
$nowbudget = $row['budget'];
$nowclicks = $row['clicks'];
$nowimpressions = $row['impressions'];

//Place into array
$myvals = array('first'=>$nowbudget,'second'=>$nowclicks,'third'=>$nowimpressions);

//senddata back
header('Content-type: application/json');
echo json_encode($myvals);


This may also help….
$sql = mysql_query("SELECT policynumuser FROM users where policynumuser = '$user_id' LIMIT 0,1");
$results = array();
while($row = mysql_fetch_array($sql))
{
$results[] = array(
'Username' => $row['policynumuser'],
'Error' => 'false',
'Message' => ''
);
}
header('Content-type: application/json');
echo json_encode($results);

Version 2 Sending side


{Do MYSQL query}
echo json_encode($row);

Version 2 Receiving side

(Without adding header(‘Content-type: application/json’);
in previous sending page.
You will need to add
var data = $.parseJSON(data);
to parse the json.


$.ajax({
url: "getmystuff.php",
dataType: "json",
type: 'GET',
data: "var1=" + mikesstuff + "&var2=" + mikestuffagain,
success: function(data){
alert(data);
var data = $.parseJSON(data);
alert(data.myclicks);
$('#amount1 span').html(data.myclicks);
} //end data

Version 3 Sending side

getmystuff.php

$results = mysql_query("SELECT * FROM table1 WHERE id = $qnum");

header('Content-type: application/json');
echo json_encode($results);

and the button clicked page that sent the request


$.ajax({
url: "getmystuff.php",
dataType: "json",
type: 'GET',
data: {accountid: accid , tagchose: tagchose},
success: function(data){
alert(data.myclicks);
$('#amount1').html(data.myclicks);
}
}); //end ajax