Jump to content

Archived

This topic is now archived and is closed to further replies.

versatilewt

Managing SQL Queries in PHP

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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Yes, I would use an include file like [b]WhyWindows[/b] said and create an array that contains all of your queries in that file. Just be sure to have .php on the end like he said too...

Share this post


Link to post
Share on other sites

×

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.