0

I want to create a search function on my site that will return strings within strings. For example: if there was a record in the SQL database where a column's value was "My name is Harry Potter", then the user could search for "Harry P" or "ott" or "My Name", and the record would come up in the search results.

1
  • 1
    You should at least accept the answer when you are satisfied. Commented Jun 23, 2012 at 19:36

4 Answers 4

2

use this in your SQL:

SELECT * FROM `table` WHERE title LIKE '%$search_word%'
Sign up to request clarification or add additional context in comments.

Comments

1
select * from your_table
where instr(some_column, 'string_to_search') > 0

Comments

1

Use LIKE function.

SELECT * FROM myTable WHERE myName LIKE '%Harry P%'

SELECT * FROM myTable WHERE myName LIKE '%ott%'

SELECT * FROM myTable WHERE myName LIKE '%My Name%'

Here I have assume myName is the field name that you have in your database & myTable is the table name.

In PHP, your query should be

SELECT * FROM myTable WHERE myName LIKE '%$yourWord%'

Comments

1

If the table is / will be small, then a statement with LIKE '%word%' will be fine.

Although you might be tempted to use a MyISAM table to be able to use a Fulltext index, this is possible in InnoDB too. It will be a bit harder to implement it this way, but it will combine the high-performance text searching that MyISAM offers with all the benefits of InnoDB (transactions, row-level locking etc.).

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.