Coding style: PHP with Complex MySQL statements



The question in short:
Do you allow MySQL to perform all functions that it can, returning
immediately the exact desired data with almost NO more processing
necessary by PHP *or* do you keep your SQL statements rather generic,
simplified, and minimal, and allow PHP to further process the received
data to specifications?

More elaborated:

I'm writing a very simple php-based web application. All it does is
post and or get data and displays it on the web.

However, the difficulty is in the MySQL statements. The statements are
(IMO) huge and quite specific (non-reuseable)! Additionally, many
statements are near identical to others, but are so complex that I see
no way to neatly introduce variables into the SQL statement (plus
wanting to avoid SQL injection...)

I am curious about others' coding techniques. In such an instance, do
you nasty-up your PHP code with many different, large SQL statements?

Do you try to shove your SQL queries into a class with function
accessibility, even tho there is little connection between one query
and another (I only see this as making an overly-complex function in
order to get to a few varieties of semi-similar SQL statements, still
needing of course many of these functions for those that completely do
not relate)?

Do you just totally extract the SQL statements, storing them in a
'define', and referencing each statement as its own constant (in which
case there will be MANY define'd SQL statements, one for every single
type of query regardless of how similiar one statement may be to
another, and therefore almost unmanageable constant names)?

Or, do you possibly not make your MySQL statements so complex and let
PHP do processing (allowing SQL statements to be very generic and
therefore minimal in quantity)?

For now, I think that last one is the big question. Which is better
style -- which is preferred -- PHP data processing or MySQL data
processing?

If the thread carries on, perhaps I can elaborate more, but in short, I
feel that anytime MySQL can process the data, further reducing
processing by PHP, it should. My reason is that, IMO, it makes your
PHP code more readable. You can read the PHP and look at the complex
SQL statement as tho it were a completely seperate module. It doesn't
matter HOW that module works, just that it gives the data you want.
And if it gives the data you want, you don't have to clutter up your
immediate coding space with further data processing. Just take your
data and go on.

I welcome any further dicussion relating to this and value anyone's
opinion that posts.

Thanks!
Stefan

.



Relevant Pages

  • Re: Coding style: PHP with Complex MySQL statements
    ... Do you allow MySQL to perform all functions that it can, returning immediately the exact desired data with almost NO more processing necessary by PHP *or* do you keep your SQL statements rather generic, simplified, and minimal, and allow PHP to further process the received data to specifications? ... the difficulty is in the MySQL statements. ... immediate coding space with further data processing. ...
    (php.general)
  • Re: newbie MySql script question
    ... how do I run this script? ... the best way to administer a mysql ... you have not written any php. ... or just start up mysql and type your SQL statements at the mysql prompt ...
    (comp.lang.php)
  • Re: Searching OO Associations with RDBMS Persistence Models
    ... I use OOPLs every day because they give me a lot of benefits. ... rules from one another was dynamic polymorhism; and that you needed PHP ... I don't like building SQL statements as string. ...
    (comp.object)
  • Re: largest php site
    ... Renault's uk site seems to cope ok running php ... Split the application and database onto different servers. ... Ensure database design is efficient, ... Learn to use stored procedures and views instead of simple SQL statements. ...
    (comp.lang.php)
  • Re: ruby based mysql sniffer
    ... want to trace the sql statements being executed by mysql as i cannot ... why don't you just turn on logging in mysql and check out the query ... since you can't debug the client app does it ...
    (comp.lang.ruby)