1

I have a PHP file to compare strings from a MySQL table and from a URL parameter.

I have detected a problem, some of the MySQL strings have blank spaces at the beginning and/or at the end of the string, and I don't know how to manage it, then sometimes the query is empty because of the blank spaces. In my case, the source string is part of a URL parameter, $id, and the string to be compared is the string value of the field titulo

$id = $_GET['id'];

$arr = array();

$rs = mysql_query("SELECT * FROM tbcoordenadas where titulo='$id'");
while ($obj = mysql_fetch_assoc($rs)){
$arr[] = $obj['procedencia'];
}
echo json_encode($arr);

I would appreciate any help to solve this issue.

EXPLANATION CONSIDERING THIS IS NOT A DUPLICATE QUESTION.

As you all may see, the question supposed to be duplicated is not considering in any answer what I am asking in my question. My question is not about removing white spaces from a string, it is about removing white spaces of a MySQL field value.

mvasco
  • 4,965
  • 7
  • 59
  • 120
  • Use [`trim()`](http://php.ne/trim): `$arr[] = trim($obj['procedencia']);` – Amal Murali Mar 22 '14 at 06:12
  • [TRIM()](http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_trim) also exists in MySQL, btw. Your bigger problem seems to be to never have heard of SQL escaping however. – mario Mar 22 '14 at 06:13
  • @AmalMurali, I guess that what you are proposing is done after comparing the strings, and not before. – mvasco Mar 22 '14 at 06:14
  • @mario,why are you marking my question as duplicate? I am not asking the same question, you have said that my problem seems to be to never have heard of SQL escaping, and in the question you consider to be duplicated there are any mention about SQL escaping.. – mvasco Mar 22 '14 at 08:28
  • There are hundreds of other potential duplicates for a shallow question like this. That doesn't mean you're entitled to have the perfect personal reference question googled for you. (More concrete advise [seems to fall on deaf ears](http://stackoverflow.com/questions/22575637/comparing-strings-containing-the-character) anyway). – mario Mar 22 '14 at 13:12

2 Answers2

3

You can use

$rs = mysql_query("SELECT * FROM tbcoordenadas where trim(titulo)='$id'");
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
1

You can use TRIM() function of mysql database.

For your case just change the query to :

SELECT * FROM tbcoordenadas WHERE TRIM(titulo)='$id';
Ariful Islam
  • 7,639
  • 7
  • 36
  • 54