Comparing two exact strings returns false

I have a variable that is submitted via an html form:

$_POST['ref'] 

And the variable retrieved from the table in the database:

 $row['ref'] 

I have a basic script comparison to check if they are the same:

 $ref = $_POST['ref']; $result = mysql_query("SELECT * FROM logbook.job"); if (!$result) { die("Query to show fields from table failed"); } $row = mysql_fetch_array($result); $refdb = $row['ref']; $refform = $_POST['ref']; echo $_POST['ref'] ."<br>". $row['ref'] . "<br><br>"; if ($refdb == $refform) { echo "Yes they are<br><br>"; } else { echo "No they are not<br><br>"; } if (is_string($_POST['ref'])) { echo "Yes"; } else { echo "No"; } echo "<br>"; if (is_string($row['ref'])) { echo "Yes"; } else { echo "No"; } 

What outputs:

 G2mtxW G2mtxW No they are not Yes Yes 

I repeat them both. Than I ask if they match. Then I check if each row is.

Why aren't they the same? How can I make them fit

Any help would be appreciated

+4
source share
3 answers

Try using a binary safe comparison for String:

result = strcmp($str1, $str2);

If the result is 0, then both are the same. Otherwise, it is not.

+7
source

One of your rows (possibly one of the databases) can be completed with a zero mark. I tested the following

 $foo = "abc\0"; $bar = "abc"; echo "$foo\n$bar\n"; if($foo == $bar) echo "Equal."; else echo "Not equal." 

Output

 abc abc Not equal. 
+6
source

Try var_dump produce both values, check their length and check them using the view source. They are somewhat different.

+1
source

Source: https://habr.com/ru/post/1380374/


All Articles