views:

44

answers:

2

I know which column should be called in a table ..for example

table name is "table1"; column name is "name"; search is "$query";

Now I query MySQL like this

mysql_query("SELECT * FROM table1 WHERE name='$query'");

Is this the right way to call?? I feel it took more time. any suggestions??

A: 

I normally do this;

mysql_query("SELECT * FROM table1 WHERE name='" . $query . "'");

But you also need to be aware of SQL injection....

Shane
but is this make any difference in query?? I mean process time??
mathew
Not really for process time though, you want to select only the columns you need rather than them all i.e. *
Shane
Just in case that didn't make sense, you want to do mysql_query("SELECT FirstName, Surname FROM table1 WHERE name='" . $query . "'");This will make a huge difference to process time.
Shane
But the difference between mine and yours?? both are almost same>>
mathew
Very true, but mine will allow you to use functions in between such escaping the characters etc... It's just my standard way of doing it.
Shane
Indexing common columns is also extremely beneficial as MJB said above...
Shane
A: 

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.

MJB
Sorry...Mine is new table and hardly any data on it. but when I uploaded to server query is slower than my local host machine. server has double capacity than my local machine. I do not have created any Index..coz table is updated every week so with an Index it may be slow. this is what I have seen from other websites.But I dont know which right and which is wrong...I can create new table again if want with Index.
mathew
@mathew: hard to say what would solve the problem, because I am not 100% sure I understand the problem. But can you tell what $query is likely to expand to? Is it simple? Does it have wildcards? These kinds of things would also help. As an aside, you can add an index without re-creating the table, and test performance that way.
MJB