Jump to content

Managing SQL Queries in PHP


versatilewt

Recommended Posts

I'm developing a script that has a large number of queries.

I was wondering what techniques people use to manage queries (i.e. include file for queries?) throughout their php code. I've just been writing them in on the pages I use and executing via ADODB, but it's making the code pretty ugly.

Thanks in advance.
Link to comment
Share on other sites

I'm not sure exactly what you're getting at. The only time I move queries to an include is if the WHERE clause gets really big and eats up a lot of page real estate. And if the query and resulting display work is only used once, I usually keep it on the same page.

However, if you're doing a ton of formatting or setup work to display the results of the query and you have several of those, that's when I start to split it out. I have several pages that are nothing but includes to different reports since I have one page that all my reports get printed from in one of my applications. Then I just include whatever report is called from the switch().

Quick question though... why use ADO? Unless you're using something like Access or MSSQL with field names > 30 characters, there's no reason not to use the built in libraries. ADO is really messy and not nearly as nice to deal with.
Link to comment
Share on other sites

I have a quite a few queries that have multiple joins, and I try to keep the SQL formatted nicely, which takes up quite a few lines.

Regarding the ADODB question, I chose it partially for the easy session management and partially because the database might be changing in the future, and didn't want to have to re-code all of the query strings.

For include files, do you name them .inc, .inc.php, .php? I have basically modules for the system, and for "contacts" for instance, i have view/edit/update pages, which all share similar queries. I'd like to stick them all in one file, however, I don't want to be wasting memory setting all of the queries for each page, if they only use a few of them That's pretty much my problem.
Link to comment
Share on other sites

I tend to use .inc.php. NEVER use .inc as if anyone stumbles across one of your include file (eg db.inc), the browser will display the contents in plain text, say goodbye to you databse username and password... At least if you use .inc.php (or.php for that matter), the browser will just show a blank page coz you haven't told the php script to echo anything.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.