Json.parse()表现不同,显然是来自2个不同页面php的相同输入

时间:2018-02-24 02:46:10

标签: javascript php json parsing encode

这是我第一次在这里问一些问题,但这个问题在我看来太奇怪了,我必须这样做。 我有一个像这样的PHP代码:

//Here i take from $_GET

if(isset($_GET["one"])){
        $one= $_GET["one"];
    }
    if(isset($_GET["two"])){
        $two= $_GET["two"];
    }
    if(isset($_GET["three"])){
        $three= $_GET["three"];
    }
//Here i process each variable for the query i want
    if($one== "any"){
        $one= "%";
    }
    else $one= "%".$one."%";
    if($two== "any"){
        $two= "%";
    }
    else $two= $two."%";
    if($three== "any"){
        $three= "%";
    }
    //

    $query = "SELECT one, two, three FROM TABLE WHERE one LIKE ? AND two LIKE ? AND three LIKE ?"

//Here i put "manually" each variable in $params
    $params = [];
    $params_type = "sss";
    $params[0] = $one;
    $params[1] = $two;
    $params[2] = $three;
    echo json_encode(exec_query_header_params($query, $params_type, $params));

为了将来的需要,我想让它变得更有活力,我改写它是这样的:

$i=0;
$params = [];
$params_type = "sss";
foreach ($_GET as $key => $value) {
    if(isset($value)){
        if($value == "any")
            $params[$i] = "%";
        else{
            if($key == "one")
                $params[$i] = "%".$value."%";
            elseif($key == "two")
                $params[$i] = $value."%";
            else 
                $params[$i] = $value;
        }
    }
    $i++;
}
echo json_encode(exec_query_header_params($query, $params_type, $params));

现在,奇怪的是代码显然在输出中给出了相同的数组,如下所示:

[["one","two","three"], //the table-head
,["aaa","bbb","ccc"],   /*
,["aaa","bab","cbc"],     the table-body
,["aaa","bgb","cgc"]]   */

但是这个ajax请求只适用于旧代码:

var query = 'getter.php?one='+one+'&two='+two+'&three='+three;
$.ajax({type:'GET', url:query, success:takeFromJSON, cache:false, async:false});
//(Inside takeFromJSON there is a call to JSON.parse(data);)

有人可以向我解释为什么会这样吗?

0 个答案:

没有答案