Jump to content


Photo

Managing SQL Queries in PHP


  • Please log in to reply
4 replies to this topic

#1 versatilewt

versatilewt
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 08 May 2006 - 07:30 PM

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.

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 08 May 2006 - 07:38 PM

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.

Info: PHP Manual


#3 versatilewt

versatilewt
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 08 May 2006 - 08:04 PM

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.

#4 haydndup

haydndup
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationJohannesburg, South Africa

Posted 08 May 2006 - 08:11 PM

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.
[!--coloro:#660000--][span style="color:#660000"][!--/coloro--]The Gene Pool Could Use A Little Chlorine[!--colorc--][/span][!--/colorc--]

#5 todd3834

todd3834
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 08 May 2006 - 08:39 PM

Yes, I would use an include file like WhyWindows 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...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users