dongpang1898 2016-07-04 13:53
浏览 8

php echo没有按预期工作

the following code is not working as expected, the echo statement never return any response to the client side and it is hang.

        $query = "INSERT INTO Tasks (ProjectID,Title,Start,End,PercentComplete,ParentID,OrderID,Summary,Expanded,LastUpdate)
        VALUES($project_Id, '$title','$start','$end','$percentComplete',$parentID,'$orderID','$summary','$expanded',NOW())";
        $result = mysqli_query($con, $query);

        if ($result) {
            $last_id = mysqli_insert_id($con);
            echo json_encode(array(ID => $last_id, Title => $title, Start => $start, End => $end, percentComplete => $percentComplete));
        }

However, if I added one more line of echo as follows, both the echo statement is able to be received on the client side.

        $query = "INSERT INTO Tasks (ProjectID,Title,Start,End,PercentComplete,ParentID,OrderID,Summary,Expanded,LastUpdate)
        VALUES($project_Id, '$title','$start','$end','$percentComplete',$parentID,'$orderID','$summary','$expanded',NOW())";
        $result = mysqli_query($con, $query);

        if ($result) {
            $last_id = mysqli_insert_id($con);
            echo json_encode(array(ID => $last_id, Title => $title, Start => $start, End => $end, percentComplete => $percentComplete));
            echo 1;
        }

I cannot figure out what is wrong here, please help to advice on this. Thanks

  • 写回答

3条回答 默认 最新

  • dqeznd1697 2016-07-04 13:59
    关注

    You are defining your array keys as constants. They need to be quoted. take a look at the following array definiton.

    $array = array(
        'id' => $id,
        'title' => $title,
    );
    

    Notice that I have used ' to quote my keys?

    You can identify your issue by enabling error reporting:

    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
    

    It will then show you an error:

    Notice: Undefined constant '...'

    In depth

    To explain a little better what's going wrong in your syntax you will first need to understand what constants are. Constants are similiar to variables. The only difference is that they have to be defined at the top of your script and that the value can not be changed (or better said is constant).

    To define a constant you would use:

    define('constantname', 'constantvalue');
    

    However, for your code to work you don't need constants but strings. What you want is to define your array keys as a string.

    'key' => 'value'
    

    I hope this will fix your problem.

    In Short

    Your echo should look like

    $json = json_encode(array(
        'ID' => $last_id,
        'Title' => $title,
        'Start' => $start,
        'End' => $end,
        'percentComplete' => $percentComplete
    ));
    echo $json;
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)