开发者

best practice for handling PHP to mysql queries? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you 开发者_C百科feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

I find myself writing very similar queries many times in an application I am making. I am looking for ideas on how I could organize and handle many different SELECT queries? I am not asking about how to optimize or secure the queries, I'm asking how I could organize multiple ones on a single page.


The simplest way is to see if you can make very similar queries into functions that take parameters.

The next simplest way is to make classes that represent logical components or "services" in your system and make methods that query things relevant to those services. One such service might be a "UserService" that has to do with user accounts. It might have a method like "getLatestRegisteredUsers($limit)". These service classes might have a small base class with some commonly used helper methods.

More complex solutions like ORMs and query builders exist, but they may well be overkill in your case.

Whatever you do, try to make small, well-defined and well-named functions and classes. A huge "functions.php" or "DatabaseClass.php" with everything randomly lumped together hardly helps anything in the long run.


I'd reccomend you look into creating a master PHP Class for your common database connections and queries.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜