Question: I'm making my search function but I don't know how to select only rows that contain the search terms. The idea would be to select only rows with the string $search from the table `lines` in the column "Text". How could I do it? Thanks

Answer: 
I don't recommend to use `LIKE` to solve substring or keyword searches, because that forces a table scan and cannot be improved by an index. Which means as your table grows, it will run hundreds or thousands of times slower than a solution using fulltext indexing.

MySQL supports a FULLTEXT index type, but it only works in MyISAM (currently). Read about how to [`CREATE FULLTEXT INDEX`](http://dev.mysql.com/doc/refman/en/create-index.html) and how to use FULLTEXT index functions [`MATCH() AGAINST()`](http://dev.mysql.com/doc/refman/en/fulltext-search.html).

Most people use [Sphinx Search](http://sphinxsearch.com/) or [Apache Solr](http://lucene.apache.org/solr) for fulltext indexing, as a complementary technology for MySQL.

See also my presentation [Practical Full-Text Search in MySQL](http://www.slideshare.net/billkarwin/practical-full-text-search-with-my-sql) or my book [SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming](https://pragprog.com/titles/bksap1/sql-antipatterns-volume-1/).
```
select * from lines where Text = $search

```

or use

```
select * from lines where Text like '%$search%' 

```

if you want to search for a value that includes $search but doesn't exactly match it.

EDIT
----

If $search only includes part of a value in Text but doesn't match the order of the words, I.E Text's value = "pizza in paris" and `$search = "pizza paris"`, then you can split $search into separate strings and run a search with an `and` clause.

```
select * from lines where Text like '%$search1%' and Text like '%$search2%'

```

where `$search1` is the first word and `$search2` is the second word.