0

I'd like to search a MySQL database to match keywords passed by the user. I heard that using LIKE is the fastest option but can't find an example of a full simple query using LIKE in PHP code.

This is what I am tring:

$value = 'Fire';
$result = mysql_query("SELECT * FROM effects WHERE title LIKE 'value%'");

I know there is an row in the database with Fire as the title but the query is returning null.

Can someone please give me an example of how to perform a MySQL LIKE search or alternative to find rows by keyword(s).

Thank you.

4

2 回答 2

3

You need to add the dollar sign of the variable:

$result = mysql_query("SELECT * FROM effects WHERE title LIKE '$value%'");
于 2012-07-01T06:31:26.913 回答
2

You are missing $ before variable value.

Your query should look like

$value = 'Fire';
$result = mysql_query("SELECT * FROM effects WHERE title LIKE '$value%'");

Say there is title like My Fire Effect. You don't want this in search result? If you want to display My Fire Effect in result too then you should use % before $value

$value = 'Fire';
$result = mysql_query("SELECT * FROM effects WHERE title LIKE '%$value%'");

Hope this helps you.

For Differences, see demo with live example

Note, first query in demo returns me 5 rows, however second query returns me 8 rows which is PERFECT.

于 2012-07-01T07:05:43.787 回答