I have a simple MySQL-based search script, I will post only the part relevant to the problem below.
$name = 'John Doe';
$query = 'SELECT username FROM members WHERE name LIKE %$name%';
Now, the problem is when I search for
John Doe
instead of getting the user with that particular name, I get all users named John
and Doe
and, the funny thing is that, it does not even put John Doe
at the top, meaning... you could find John xxxx
before that.
Is there and advanced MySQL attribute to accomplish this task?
change
$query = 'SELECT username FROM members WHERE name LIKE %$name%';
to
$query = 'SELECT username FROM members WHERE name LIKE "'.%$name%.'"';
or
$query = "SELECT username FROM members WHERE name LIKE '%$name%'";
note, that changing to
$query = 'SELECT username FROM members WHERE name LIKE "%$name%"';
will NOT do the trick.
This is because single quoted strings do not interpret variables.
Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
Double quoted strings however, will.
The most important feature of double-quoted strings is the fact that variable names will be expanded. See string parsing for details.
0 comments:
Post a Comment