Are you saying that it took too long to return the rows you wanted? If that is the case, it has nothing to do with how you are forming your SQL, and everything to do with what SQL you end up with.
For example, in your case, you wrote:
select * from table1 where name='$query'
Assuming that works and expands to what you want (and I don't think it will -- see Keyzo's answer for why), you have to look at the definition of the table and its indexes to determine if that is a fast way to do it. Specifically, is there an index on name
? If there is an index, how selective is it? How big is the table? More than 100K records? More than a million? Without knowing these details it will be hard to troubleshoot.