php - json_encode returning null -
echo json_encode(array(utf8_encode("success")));
that code returning null, trying debug project , homecoming post variable, won't work string
heres total code:http://www.bludevelopment.com/php/getdata.txt
the problem in uploadinstalldata function. app calls separately each function.
any help much appreciated!
function uploadinstalldata(){ if (isset($_post["timestamp"]) && isset($_post["pmino"]) && isset($_post["gps"])){ //$result = array(utf8_encode('value', 'success')); if ($_post['cmd'] == "uploaddata"){ $con = mysql_connect("localhost","bludevel_pmi","password1"); if (!$con) { die('could not connect: ' . mysql_error()); } mysql_select_db("bludevel_pmiform", $con); $sql="insert jobdata (starttime, pmino, address, beforephoto, extraphoto1, extraphoto2, extraphoto3, abletoinstall, installproblem, bboxstatus, newlocation, bboxphoto, occupied, basementfinished, buildingtype, servicesize, servicetype, housepipesize, housepipetype, sscontrolvalve, newmeter, newmetersize, newtransmitter, miulocation, meterinstalltype, mtrlocated, mtrdirection1, mtrsideof1, mtrdistance, mtrdirection2, mtrsideof2, accessnotes, bldgwidth, bldgdepth, review, streetvalve, housevalve, authorizedwork, inspectorsname, newstreetvalve, addpiping, installedby, afterphoto, afterphoto2, customerincentive, confirmsignal, installnotes, endtime, gps) values ('".$_post[timestamp]."', '".$_post[pmino]."', '".$_post[address]."', '".$_post[beforephoto]."', '".$_post[extraphoto1]."', '".$_post[extraphoto2]."', '".$_post[extraphoto3]."', '".$_post[abletoinstall]."', '".$_post[installproblem]."', '".$_post[bboxstatus]."', '".$_post[newlocation]."', '".$_post[bboxphoto]."', '".$_post[occupied]."', '".$_post[basementfinished]."', '".$_post[buildingtype]."', '".$_post[servicesize]."', '".$_post[servicetype]."', '".$_post[housepipesize]."', '".$_post[housepipetype]."', '".$_post[sscontrolvalve]."', '".$_post[newmeter]."', '".$_post[newmetersize]."', '".$_post[newtransmitter]."', '".$_post[miulocation]."', '".$_post[meterinstalltype]."', '".$_post[mtrlocated]."', '".$_post[mtrdirection1]."', '".$_post[mtrsideof1]."', '".$_post[mtrdistance]."', '".$_post[mtrdirection2]."', '".$_post[mtrsideof2]."', '".$_post[accessnotes]."', '".$_post[bldgwidth]."', '".$_post[bldgdepth]."', '".$_post[review]."', '".$_post[streetvalve]."', '".$_post[housevalve]."', '".$_post[authorizedwork]."', '".$_post[inspectorsname]."', '".$_post[newstreetvalve]."', '".$_post[addpiping]."', '".$_post[installedby]."', '".$_post[afterphoto]."', '".$_post[afterphoto2]."', '".$_post[customerincentive]."', '".$_post[confirmsignal]."', '".$_post[installnotes]."', '".$_post[endtime]."', '".$_post[gps]."')"; echo json_encode(array(utf8_encode("success"))); homecoming true; $res = mysql_query($sql); if (!res) { die('error: ' . mysql_error()); } if (!$mysql->error) { } mysql_close($con); } }}
as mentioned, should not using ext/mysql
extension new code. until purged ancient php tutorials on internet, wont see end of people going "why doesn't code work more", because using throws e_deprecated
in php 5.5, , removed in later versions.
this uses mysqli extension, prepared queries (you can utilize pdo).
$db = new mysqli($dbip, $dbuser, $dbpass, $dbname); if ($query = $db->prepare("insert table_name (x, y) values (?, ?)")) { $query->bind_param("ss", $_post["x"], $_post["y"]); // s = string, = int $query->execute(); } else { echo json_encode(array("error" => "malformed query")); homecoming false; // in php, false or null both signify error in non-boolean context } echo json_encode(array("success" => "query attempted")); homecoming true;
of course, don't want blindly assume query successful here (hence "attempted").
your json doesn't seem failing, best give values in array associative key. also, utf8_encode
ing array gives null
. see this:
json_encode(array("query attempted")); // ["success"] json_encode(array("success" => "more info")); // {"success":"more info"} json_encode(utf8_encode(array("success" => "more info"))); // null
doing improve practice , can help debug complex json when can homecoming many different things. "null" part answers question pretty much this.
also, iso-8859-1
(aka latin1
) subset of unicode (as ascii); encode cleanly json. should utf8_encode
stick in json explicitly, though (such user input).
another vital thing missed when first answered (i can't believe missed this):
. $_post[newlocation] .
this isn't proper php. might work depending on how strictly php configured, keys in arrays quoted strings, not unquoted constants. can , throw error in error log (undefined t_constant newlocation; 'newlocation' assumed
).
use $_post["newlocation"]
(single or double quotes work, remember have different meanings in php)
php mysql json
No comments:
Post a Comment