user188995
user188995

Reputation: 557

PHP SQL Multiple autocomplete search

currently I've written a code and it works fine with only keyword. Also, it doesn't take care of multiple entries. for example, if I have the keyword "blue" twice in DB; it shows "blue" twice in the search input box when I start typing "blue". Rest of the code works fine. How should I tweak my code? Also, if a column has "blue" & "green" as the row; it shows the complete thing: "blue & green". My php code:

<?php
$keyword = mysql_real_escape_string($_POST['keywords']);
$sql = "SELECT * FROM job WHERE work='$keyword' or work LIKE 'ANOTHER_PARAMETER' LIMIT 5";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
echo //details and run the loop 
?>

Upvotes: 2

Views: 572

Answers (3)

xan
xan

Reputation: 4696

You can try modifying your SQL DB. You can have a column of "work_ids" and another for "work corresponding to those ids".

Sorry, I left a part of your question unanswered but thanks to @T-shirt Dude; I remebered it instantly. If I am reading your question right, you want to search for multiple keywords in a single column? If so you can do:

$sql = mysql_query("SELECT work_id, work FROM job WHERE work like '%$q%' OR work like 'ANOTHER_PARAMETER'
ORDER BY work_id LIMIT 10");

You can put as many "OR"s that you want.

Upvotes: 1

Ed Meacham
Ed Meacham

Reputation: 583

I'm not sure I completely follow--seeing the code that creates response might help us a little more.

At any rate, from the way I'm understanding your problem, it sounds like you might need a group by.

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80639

Considering the work to be the column with the values 'blue' and 'green' in it, the code should be:

"SELECT * FROM job WHERE work='$keyword' or work LIKE 'ANOTHER_PARAMETER' GROUP BY work LIMIT 5";

If that's not the case, I was unable to understand your question.

Upvotes: 1

Related Questions