简体   繁体   English

来自PHP的响应未如预期

[英]Response from PHP not as expected

I am trying to send data from a database to an app based on what was send from the app to the server like so: 我正在尝试根据从应用程序发送到服务器的内容将数据从数据库发送到应用程序,如下所示:

$search_bp=$_POST['search_bp'];
$search_trig = 0;

    if(strcmp($search_bp,"1")==true){
    //decisions that could set search_trig = 1;
    //eg
    if(strcmp($_POST['EStype'],'Event')){
       if(strcmp($_POST['type'],'Any')==false){
           if(strcmp($_POST['type'],$row["type"])==false){
           $search_trig=1;//doesnt match specs
          }
      }
    }   
    }
if($search_trig == 0){
    $event["pid"] = $row["pid"];
    $event["name"] = $row["name"];
    $event["longitude"] = $row["longitude"];
    $event["latitude"] = $row["latitude"];
    $event["pavement"] = $row["pavement"];
    $event["traffic"] = $row["traffic"];
    $event["environment"] = $row["environment"];
    $event["image_b64"] = $row["image_b64"];
    $event["date"] = $row["date"];
    $event["time"] = $row["time"];
    $event["type"] = $row["type"];
    // push single product into final response array
    array_push($response["events"], $event);
}
    $response["success"] = 1;
    // echoing JSON response
    echo json_encode($response);

What is going wrong is that it strcmp($search_bp,"1") seems to always be false even though I am sending it as 出问题了,即使我将其发送为strcmp($search_bp,"1")似乎总是错误的

params.add(new BasicNameValuePair("search_bp",  Integer.toString(search_bp)));

Where I know search_bp=1 , I just don't know too much about php so I'm pretty sure it is just my syntax. 在我知道search_bp=1 ,我对php不太了解,所以我很确定这只是我的语法。

Thank you in advance, 先感谢您,

Tyler 泰勒

strcmp returns an integer not a boolean. strcmp返回一个整数,而不是布尔值。 Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Technically, you should simply be checking as if ($search_bp == "1") { 从技术上讲,您应该简单地检查if ($search_bp == "1") {

If we were to dissect your statement in php's loose type world you can see your mistake. 如果我们要在php的松散类型世界中剖析您的陈述,您会发现您的错误。

`strcmp` will return 0 because they do match. 
0 = false 
1 = true

so if(strcmp($search_bp,"1")==true){ solves to if(0==true){ , then if(0==1){ which would not meet the condition. 所以if(strcmp($search_bp,"1")==true){求解为if(0==true){ ,则if(0==1){不会满足该条件。

Please Note That strcmp not return true or false: 请注意, strcmp不会返回true或false:

It return as follow 返回如下

0 - if the two strings are equal
<0 - if string1 is less than string2
>0 - if string1 is greater than string2

You need to change something like below: 您需要更改以下内容:

if(strcmp($search_bp,"1")==0){
//
}

In your case you can also use == operator. 您也可以使用==运算符。

Please Note : == only returns true or false , it doesn't tell you which is the "greater" string 请注意: ==仅返回truefalse ,它不会告诉您哪个是“更大”的字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM