0

I have a MySqL DB with a table of Properties NAMES(can be more then one word) and I want to run a query to get results for the user text inputs to use for an autocomplete field.

The query I currently use is:

$query = "SELECT * FROM table WHERE LOWER(name) LIKE '%$value%' LIMIT 7";

But it is not good enough.

I tried splitting the input $value but for some reason is not working:

$values = explode(" ", $value);

$str = "";
for($i = 0; $i < count($values); ++$i)
{
    if( $i == 0)
        $str .= "LOWER(name) LIKE '%&$values[$i]%' ";
    else
        $str .= "AND LOWER(name) LIKE '%$values[$i]%' ";
}

$query = "SELECT * FROM table WHERE ". $str . " LIMIT 7";

Do you have any suggestions?

TX in advance.

2
  • WARNING: This has some severe SQL injection bugs because $_GET data is used inside the query. Whenever possible use prepared statements. These are quite straightforward to do in mysqli and PDO where any user-supplied data is specified with a ? or :name indicator that’s later populated using bind_param or execute depending on which one you’re using. NEVER put $_POST or $_GET data directly in your query.
    – tadman
    Dec 11, 2016 at 20:39
  • @freewind did my answer helped?
    – divix
    Dec 13, 2016 at 20:41

1 Answer 1

0

Do OR instead of AND in your query:

$str .= "OR LOWER(name) LIKE '%$values[$i]%' ";
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.