wtsang02
wtsang02

Reputation: 18863

search a string with spaces in sql with php

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.

Upvotes: 0

Views: 1311

Answers (1)

Tim Withers
Tim Withers

Reputation: 12059

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.

Upvotes: 2

Related Questions