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.
精彩评论