Page 1 of 1

Building SQL queries - is there an easier way?

Posted: Fri Sep 19, 2008 11:24 am
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

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

Posted: Fri Sep 19, 2008 11:47 am
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.

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

Posted: Sat Sep 20, 2008 9:21 am
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!