2

I'm experiencing unexpected results from a query when using numbers as pattern. The query looks like this:

SELECT Code FROM table WHERE Code LIKE '%xxx%'  

Everything is fine when xxx are letters, but when they are numbers the query returned by the remote PHP is wrong. It seems as if the starting %nn is interpreted as an ASCII code.

Example:

SELECT Code FROM table WHERE Code LIKE '%26F%'  

is returned as: SELECT Code FROM table WHERE Code LIKE '&F%'

Javascript code sample:

find = "26F";
srch = "Code LIKE '%" + find + "%'";
url: "load_searched_record.php?target="+srch;
...

PHP simplified code:

$target = $_GET['target'];
echo $target;

Since I removed all the MySql code for testing, leaving only the above 2 rows, it is quite evident that the problem in originated from PHP, but why?
Should I encode in same way the query? If yes, how? Thanks

Pls note that the PHP file is UTF-8 encoded.

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
Orionis
  • 983
  • 7
  • 11

1 Answers1

7

That’s because you failed to URL-encode the values you are putting into the query string properly … (and that is not PHP’s fault.)

You need to use encodeURIComponent on the value, before you append it to the URL:

"load_searched_record.php?target="+encodeURIComponent(srch)

CBroe
  • 91,630
  • 14
  • 92
  • 150