duanqun7761
2012-06-21 23:33
浏览 120
已采纳

用php在sql中搜索带空格的字符串

I need to search a string in mysql with php. I get an error related to the spaces in the string. I an not fimilar with regex, I am not sure it that is my only choice. example:

$ex="This and That";

$sql = 'SELECT
some_ID
FROM ' . atable. ' WHERE ' . strings. ' LIKE ' . $ex. ' AND visable=' . '1';

after executing I get an error like:

"near 'That AND visable=1' at line x"

so its probably not picking up the first two words, any suggestions? Thanks in advance.

图片转代码服务由CSDN问答提供 功能建议

我需要用php搜索mysql中的字符串。 我收到与字符串中的空格相关的错误。 我与正则表达式并不相似,我不确定这是我唯一的选择。 example:

  $ ex =“This and That”; 
 
 $ sql  ='SELECT 
some_ID 
FROM'。 一张桌子。  '在哪里'。 字符串。  ' 喜欢 ' 。  $前。  '和visable ='。  '1'; 
   
 
 

执行后我收到如下错误:

“near”那个AND 第x行的可见= 1'

所以它可能没有拿到前两个单词,有什么建议吗? 提前谢谢。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushi7314 2012-06-21 23:36
    已采纳

    You are missing quotes around the string. They need to be encapsulated entirely for the query to execute properly.

    Change this:

    LIKE ' . $ex. ' AND
    

    To this:

    LIKE "' . $ex. '" AND
    

    On a side note, make sure you are protecting your self against SQL injections AND make sure your query is properly escaped.

    点赞 打赏 评论

相关推荐 更多相似问题