Rafael Shkembi Rafael Shkembi - 5 months ago 19
SQL Question

if condition does not return true php

i have a variable named

esito
. This variable is going to take 2 values
OK
or
ko
this depends from the value in the database. Now i'am making the query and i get the value from the db "In our case is ko"

$query="select esito from credit_check where piva is not null and piva='1234656'";
$res_check = $mysqli->query($query);
while($row = mysqli_fetch_assoc($res_check)){
$esito = $row['esito'];
}


if i echo the
$esito
i get "ko"

Now i have this if else condition. if the
$esito
is "ko" i will echo "KO" and else i echo "OK".

if($esito == "ko"){
echo "KO";
} else {
echo "OK";
}


After the if else condition i get "OK". How is this possible?? I sould get "KO" not "OK" i have trying to figure out for the past 30 minutes but i havent found the problem. Thanks all

Update
i have remove the while loop

$row = mysqli_fetch_assoc($res_check);
$esito = $row['esito'];


the
var_dumb($esito)
result is


/var/www/html/credit_check_bo_bt.php:21:string 'ko
' (length=3)


again if i echo the
$esito
i get "ko"

if condition is still
false

Answer

Making this a community wiki answer. I have nothing to gain from this.

I'am sorry i have forgot to change the value of piva i get this in var dumb /var/www/html/credit_check_bo_bt.php:21:string 'ko ' (length=3) – Rafael Shkembi 15 mins ago

There you have it, you have a trailing space in your db's value.

Either trim() it, or remove it from your db.

If removing it is impossible because you have too many, then trim() is the way to go.