trevor1940
asked on
Encoding number to JSON in PHP
Hi this is follow on from Handling-ZERO-Results-in-a -PHP-PDO-Q uery
The code bellow is representation of what I'm doing only
When I return a row from a PostGreSQL database some of the columns represented as latitude & Longitude stored as datatype 'real' in the database when these are encoded to json as a string so
in the databse DB_Long = 12.34 (Digital Lognitude)
in the encoded JSON DB_Long = "12.34"
DataBase integers are being treated a numbers in the JSON
This is causing errors when trying to parse the JSON
Any suggestions how to resolve?
The code bellow is representation of what I'm doing only
When I return a row from a PostGreSQL database some of the columns represented as latitude & Longitude stored as datatype 'real' in the database when these are encoded to json as a string so
in the databse DB_Long = 12.34 (Digital Lognitude)
in the encoded JSON DB_Long = "12.34"
DataBase integers are being treated a numbers in the JSON
This is causing errors when trying to parse the JSON
Any suggestions how to resolve?
try
{
$pdo = PDO(Enter DB Connect details);
}
catch (PDOException $exec)
{
trigger_error($exec->getMessage(), E_USER_ERROR);
}
$sql = <<<SQL
select "Place_Name","DB_Long","DB_Lat,"some_string", "Interger" from table where "Place_Name" = ?
SQL;
$sth= $pdo->prepare($sql);
$execute(array($_POST['MySearchVal'];
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
$data=array();
foreach($result as $row)
{
$data=array(
'Place_Name' => $row['Place_Name'],
'DB_Long' => $row['DB_Long'],
'DB_Lat' => $row['DB_Lat'],
'some_string' => $row['some_string'],
'Interger' => $row['Interger'];
);
}
echo json_encode($data);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi doing this worked
However in FireBug > net
there is no JSON tab but the response is processed as JSON??
Chris said
How would you know if only had 1 row without doing something like this?
$res = $sth->fetchAll(PDO::FETCH_OBJ);
echo json_encode($res);
However in FireBug > net
there is no JSON tab but the response is processed as JSON??
Chris said
If you only have one record returned, then just do:
$result = $sth->fetch(PDO::FETCH_ASSOC);
echo json_encode($result);
If you have an array of records, then just this:
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($results);
How would you know if only had 1 row without doing something like this?
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
$numRows =count($result)
if($numRows == 1)
{
$result = $sth->fetch(PDO::FETCH_ASSOC);
echo json_encode($result);
}
elseif($numRows > 1)
{
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
}
else
{
// nothing returned
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I didn't think about LIMIT 1 but I thought I was missing something in PHP hence my question
Has for FireBug i'm aware if the return is JSON the JSON tab is shown my point is it wasn't shown by doing this in PHP just wondered why
Has for FireBug i'm aware if the return is JSON the JSON tab is shown my point is it wasn't shown by doing this in PHP just wondered why
$results = $sth->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
If your script is called via AJAX, then you'd need to tell your AJAX script that you were getting JSON back. This then sets up the header so the browser knows it's recieving JSON, and the JSON tab will then be available.
If you're not calling this via AJAX and you're just loading it in your browser, then your PHP script should set the header manually:
If you're not calling this via AJAX and you're just loading it in your browser, then your PHP script should set the header manually:
header('Content-Type: application/json');
echo json_encode($result);
Unless headers are set (either by the AJAX call or your script), then the browser just sees the data as text and your JSON tab won't show.
ASKER
Hi Chris
Sorry for going off topic
AJAX call is written like this which sets the headers to JSON so why do I not see a JSON tab in FireBug?
Sorry for going off topic
AJAX call is written like this which sets the headers to JSON so why do I not see a JSON tab in FireBug?
var MyLink = "path/to/MyQuery.php?serech=SearchType&" $('MyDiv').find('input').serialize();
$.ajax({
url: MyLink,
async: false,
ContentType: 'application/json',
dataType: 'json',
success: function(data){
// Do Stuff
}
});
Not sure. When you click on the Console tab in Firebug, you should see your request. Something like this:
+ GET path/to/MyQuery.php?serech =SearchTyp e&...
If you click on the little 'plus' sign to the left of the request, it should expand out and then show you some more tabs underneath it - Params, Headers, Response, HTML and JSON. Clicking on the JSON tab should show you the JSON data. If you don't have a JSON tab, then the data's probably not coming back as JSON. If that's the case, click on the Response tab and see what is coming back - you may need to tweak your server script slightly.
On another point - you shouldn't need the ContentType option in your AJAX call - that's for telling the server the format of the data your sending to your script - you're not sending JSON, so it doesn't need to be there. Also, is there a specific reason why you're turning asynchronous mode off - it will slow down your script execution.
+ GET path/to/MyQuery.php?serech
If you click on the little 'plus' sign to the left of the request, it should expand out and then show you some more tabs underneath it - Params, Headers, Response, HTML and JSON. Clicking on the JSON tab should show you the JSON data. If you don't have a JSON tab, then the data's probably not coming back as JSON. If that's the case, click on the Response tab and see what is coming back - you may need to tweak your server script slightly.
On another point - you shouldn't need the ContentType option in your AJAX call - that's for telling the server the format of the data your sending to your script - you're not sending JSON, so it doesn't need to be there. Also, is there a specific reason why you're turning asynchronous mode off - it will slow down your script execution.
ASKER
Perplexing!
Added this to PHP script and adjusted AJAX Call
Still no JSON tab
Copied response into a txt file this validates / opens in JSON parsons
Gone back over previous APPs including Jquery autoresponce none of these are showing JSON tab
Don't remember a specific application where I can be 100% sure i've seen it on this system
Added this to PHP script and adjusted AJAX Call
header('Content-Type: application/json');
echo json_encode($result);
Still no JSON tab
Copied response into a txt file this validates / opens in JSON parsons
Gone back over previous APPs including Jquery autoresponce none of these are showing JSON tab
Don't remember a specific application where I can be 100% sure i've seen it on this system
ASKER
FYI
Created simple test page at home this retrieves JSON data from a local file
The JSON tab is showing so will run it tomorrow see what happens
Created simple test page at home this retrieves JSON data from a local file
The JSON tab is showing so will run it tomorrow see what happens
ASKER
Thanx for your help solved the original issue
ASKER
When I get back in the office i'll test what you said thanx for quick reply