2012-06-21 23:33
浏览 120


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
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 
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.

    点赞 打赏 评论

相关推荐 更多相似问题