json_encode返回null

时间:2013-02-18 20:17:49

标签: php mysql json

echo json_encode(array(utf8_encode("success")));

该代码返回null,我正在尝试调试项目并希望它返回一个post变量,但它甚至不能用字符串

继承完整代码:http://www.bludevelopment.com/php/getdata.txt

问题出在uploadinstalldata函数中。 该应用程序会为每个功能单独调用它。

任何帮助都得到了很多赞赏!

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 INTO 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")));
return true;
$res = mysql_query($sql);
if (!res)
  {
  die('Error: ' . mysql_error());
  }

if (!$mysql->error) {
}

mysql_close($con);
}
}}

1 个答案:

答案 0 :(得分:3)

正如我所提到的,您不应该使用ext/mysql扩展名来获取新代码。在从互联网上所有那些古老的PHP教程中清除它之前,我们不会看到人们的结尾“为什么我的代码不再工作”,因为使用它会在PHP 5.5中抛出E_DEPRECATED,它将会在以后的版本中完全删除。

这使用mysqli扩展名prepared queries(您也可以使用PDO)。

$db = new mysqli($dbip, $dbuser, $dbpass, $dbname);

if ($query = $db->prepare("INSERT INTO table_name (x, y) values (?, ?)")) {
    $query->bind_param("ss", $_POST["x"], $_POST["y"]); // s = string, i = int
    $query->execute();
} else {
    echo json_encode(array("error" => "malformed query"));
    return false;
    // in PHP, false or null both signify an error in a non-boolean context
}
echo json_encode(array("success" => "query attempted"));
return true;

当然,你不要盲目地认为查询是成功的,就像我在这里做的那样(因此“尝试”)。

您的JSON实际上似乎没有失败,但最好是为数组中的值提供关联键。此外,utf8_encode数组会给出null。见:

json_encode(array("query attempted")); // ["success"]
json_encode(array("success" => "more info")); // {"success":"more info"}
json_encode(utf8_encode(array("success" => "more info"))); // null

这样做是更好的做法,可以帮助调试复杂的json,它可以返回许多不同的东西。回答你问题的“空”部分就是这个。

此外,ISO-8859-1(又名latin1)是Unicode的子集(与ASCII一样);它将干净地编码为JSON。但是,您应该utf8_encode显式地使用JSON(例如用户输入)。


当我第一次回答时,我错过了另一个重要的事情(我不敢相信我错过了这个):

. $_POST[NewLocation] .

这不是正确的PHP。它可能会工作取决于PHP的配置严格程度,但数组中的键被引用为字符串,而不是作为常量引用。这可能并且将在错误日志(Undefined T_CONSTANT NewLocation; 'NewLocation' assumed)中抛出错误。

使用$_POST["NewLocation"](单引号或双引号有效,但请记住它们在PHP中有不同的含义)