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.
4 Answers
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
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.).