3

I was going to use the scuttle solution on: http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html for handling searches on my website. I was wondering how I could take the search input from a user and turn it into a single query.

For instance, let's say a user inputted 'blue dogs' in their search query... How could I dynamically update the query to include ('blue', 'dogs') in union and intersection queries?

3 Answers 3

1

The methods posted here are correct, just one little security addition: Don't forget to escape the user inputted data, otherwise you're just one step away from SQL injections. Or avoid the query and use prepared statements instead: PHPManual Prepared Statements

Sign up to request clarification or add additional context in comments.

Comments

0

You can do like:

$search_string = implode(',', $search_array);

Now in your query you can use the IN clause:

$query = "select * from table where field IN ('".$search_string."')";

1 Comment

-1: String parameters should be quoted, all values should be filtered through mysql_real_escape_string
0

for example your user input is "blue dogs", then on the page

$searchstring = "blue dogs"; // or fetch the input
$arr = explode(" ",$searchstring); //this is explode the text by every "space" character

you have the user inputed string in array $arr now, now use it in query like you usually do

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.