0

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.

4
  • Always output your query with echo $sql; before you ask question here - in 90% you would solve your issue yourself Commented Jun 21, 2012 at 23:37
  • Where is the mysql_real_escape_string()? Protect yourself from SQL injection, it's a lot cheaper than having your website hacked. php.net/mysql_real_escape_string Commented Jun 21, 2012 at 23:38
  • @Jasper: OP has reached his cap of mysql_real_escape_strig() for today Commented Jun 21, 2012 at 23:40
  • Thanks for the link. I just finished reading. In my case user select a number, and the number will phrase to a String-with-space. and it is done server sided. So im guessing I don't need mysql_real_escape_strig() ? Thanks in advance Commented Jun 21, 2012 at 23:49

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.