开发者

which is the right Query for MySQL

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'");
开发者_如何学JAVA

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


I normally do this;

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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜