AR.
AR.

Reputation: 1955

string variable shows wrong length

So I have a function that gets some string variables from a form (after the form is submitted), then runs a query using those variables and returns lines of results. I have several conditions that work fine, except for one and I can't figure out what's wrong. When I print out the query and run it in phpMyadmin - it works just fine (returns 3 rows for example), but it doesn't run on the page (shows that 0 rwos are returned). One thing that I have noticed is that when I do var_dump it gives the correct type (string) but wrong length. trim doesn't make any difference. So fo example

$name2 = "John Doe";
var_dump($name1);
var_dump($name2);

The name2 returns string(8) But when for name1 it returns string(9), even though name1 is also "John Doe" - I have no idea what that extra character is. That variable is coming from a form, from a select element. Select is populated from a table. I trim resulting POST value before assigning to name1. Character encoding is the same for the table where names in select element are coming from and table on which I run a query. All other variables from the form are passing fine and query runs correctly if I don't add the name. Again, if I print out the query (with name condition included) and copy and paste into phpMyAdmin - it runs just fine. I'm going crazy here.

Upvotes: 1

Views: 1958

Answers (3)

Eineki
Eineki

Reputation: 14909

At a first look this can be a character encoding issue.

I don't know what happens (there are to few data to me) but I would suggest to scan the two string (the actual one and the string you are waiting for) and have the chars returned for further investigation:

function compare($s1, $s2) {
    $i = 0;
    while ($s1[$i]) {
        if ($s1[$i] != $s2[$i]) return array($s1[$i], $s2[$i]);
        $i++;
    }
}

var_dump(compare($name1, $name2));

Edit: Maybe a couple of php lines will help me to better explain what I mean. The strings look equals but they are not as the A in the second one is a Cyrillic Character that is rendered on the screen as an A (but counts two bytes, hence the different strlen)

 mb_internal_encoding('UTF-8');
 $plainOldAscii = 'TASTE';
 $aBitOfCyrillic = html_entity_decode('TАSTE', ENT_NOQUOTES,'UTF-8');
 echo "$plainOldAscii  -> " . strlen($oldPlainAscii)  
    . " -> "                  . mb_strlen($oldPlainAscii)
    . "\n$aBitOfCyrillic -> " . strlen($aBitOfCyrillic) 
    . " -> "                  . mb_strlen($aBitOfCyrillic)
    . "\n";

Upvotes: 2

AR.
AR.

Reputation: 1955

Ok, it was a database issue. Still have no idea how is that possible - there were no extra spaces in names in the db or strange characters. Encoding is correct and the same thoughout the db, but printing out names on the screen and then copying and inserting back in the names table fixed it. Even though there was no visible difference. Weird.

Upvotes: 0

davidaam
davidaam

Reputation: 449

Possibly your $name1 is something like "John Doe " with a space at the end or a space at the beginning...

Upvotes: 0

Related Questions