Building SQL queries - is there an easier way?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
deafpanda
Forum Newbie
Posts: 7
Joined: Thu Jul 31, 2008 5:25 pm

Building SQL queries - is there an easier way?

Post by deafpanda »

Hi all.

I've been struggling with writing a script to query a mysql table by lots of different criteria - generating SQL like this:

SELECT field1, field2, field3, field4, field5 FROM table1 WHERE field4>6 AND field5='value1' ORDER BY field1 DESC LIMIT 0,10

depending on form input.

Is there a class out there that makes this any easier? At the moment I've got about 200 lines of code that aren't reusable for anything else.

Does anyone think a class would help matters, or would it be pointless?

Yours interestedly,

deafpanda
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Re: Building SQL queries - is there an easier way?

Post by Maugrim_The_Reaper »

Depends on the specific pain you feel ;). Is it building lengthly hard to maintain queries, or just getting the form data into whatever you use to build the query? PHP has lots of both Form managers and also the commonly used Database Abstraction/ORM libraries for both needs.
deafpanda
Forum Newbie
Posts: 7
Joined: Thu Jul 31, 2008 5:25 pm

Re: Building SQL queries - is there an easier way?

Post by deafpanda »

Well, my specific pain is that I'm making search forms for several different tables on a database. Users can search by up to five criteria on each table, choose operators (less than, greater than, equal to). Writing all the code for a search form for one table took me quite a while, I'm loathe to write similar code for the others.

I don't imagine database abstraction layers can help me with this?

Thanks for your help!
Post Reply