dongxiangshen7916
2014-03-22 06:11 阅读 63
已采纳

删除MySQL字段值中字符串值的开头或结尾处的空格[复制]

This question already has an answer here:

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.

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongshi8359 dongshi8359 2014-03-22 06:13

    You can use

    $rs = mysql_query("SELECT * FROM tbcoordenadas where trim(titulo)='$id'");
    
    点赞 评论 复制链接分享
  • duanposhi3641 duanposhi3641 2014-03-22 06:20

    You can use TRIM() function of mysql database.

    For your case just change the query to :

    SELECT * FROM tbcoordenadas WHERE TRIM(titulo)='$id';
    
    点赞 评论 复制链接分享

相关推荐