0

My if statements are not working despite the log file confirming that the values meet the conditions required.

As you will see below I have attempted to use both boolean and numerical values (as I have read that there are a few quirks with boolean statements in PHP.)

  $lift = isset($p["lift"]) ? $p["lift"] : 0;
  $parking = isset($p["parking"]) ? $p["parking"] : false;

// LIFT
  if ( $lift === 1 && $home ) {
    $query .= " AND `lift` == $lift";

  }

  // PARKING
  if ( $parking === 1 && $home ) {
    $query .= " AND `parking` != '';";
  }


  $log_file = "../../queries.log"; 
  $error_message = "query: '$query' \n\n lift: ".$lift."\n home: ".$home."\n";  
  error_log($error_message, 3, $log_file);

I have tried both double and triple equal operators without success. I have tried both boolean and numerical values. The log statement prints the following:

'SELECT id, ref_crm, `type`, prov_name, prov_id, muni_name, muni_id, barrio, price_latest, photo,sqm,bed,bath,lift,parking,`year`,descr,
            x(pt) as lat, y(pt) as lng, ref_cat FROM outlet WHERE prov_id = '06' AND `type` = 'Piso' AND price_latest >= 0 AND price_latest <= 500000 AND sqm >= 0 AND sqm <= 200'

 lift: 1

 home: true 

As you can see, the string statements are not being attached to the query despite the two conditions both being met.

I have also tried removing the variables I've created ($lift and $home) and simply used $p["lift"] and $p["parking"] without success. The only way I am able to make this work is to specifically state $lift === 1 and $home === true (double or triple equal operators) above the conditions. This despite the log confirming that these variables already have those values set! I have also tried double and triple equal operators with $home and $p["home"]

7
  • 1
    Where is the $home variable coming from? Commented Sep 20, 2019 at 7:47
  • 1
    try (int)$lift === 1 and (int)$parking === 1 Commented Sep 20, 2019 at 7:49
  • 2
    there are two possibilities 1. $home is not define 2. $lift & $parking is string and you comparing with int (try ==). Commented Sep 20, 2019 at 7:51
  • 1
    You do not use a double = in sql, just singles so AND lift = $lift rather than AND lift == $lift etc Commented Sep 20, 2019 at 8:01
  • 1
    Solved by Shanteshwar Inde. The problem was that the values were strings. This was not apparent by simply reading the log file Commented Sep 20, 2019 at 8:42

1 Answer 1

1

Try echoing something out within your if statements.

Also please note:
https://www.php.net/manual/en/language.operators.comparison.php

Solution:

if (!empty($home) && $lift == 1) {
 echo 'Lift works';
} else {
 echo 'Lift is not 1';
}

if (!empty($home) && $parking == 1) {
 echo 'Parking works';
} else {
 echo 'Error: home parking is not 1';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.