Reputation: 2007
I have this piece of code here:
$query = mysql_query("SELECT * FROM example WHERE text LIKE '%$value%'");
Would it make a difference if I would use:
$query = mysql_query("SELECT * FROM example WHERE text LIKE '$value'");
If yes, what would it be? What would be the difference?
Upvotes: 3
Views: 482
Reputation: 1907
The difference is that '%$value%' will seach for matches containing $value (for exemple if $value = 'foo' it could return 'foobar' or 'barfoobar'), '$value' only matches the exact value of $value.
Upvotes: 2
Reputation: 4454
Yes this has nothing to do with php, its a sql thing, % means like a wildcard there could be 0 or more characters instead of it.
%abc% matches abc, aabca, aabc, abcd
%abc matches dabc, abc but not abcd or tabcd
abc% matches abcd, abc but not dabc, tabcd
Upvotes: 9
Reputation: 13485
In PHP there is no difference (although you might want to take a look at using PDO for your database queries), the '%' symbols affect the query executed in MySQL.
% acts as a wildcard so the first result will return anything that contains the term 'value' in its text attribute, whereas the second will return only records that match exactly the term 'value'
Upvotes: 1