Re: Coding style: PHP with Complex MySQL statements



s1037989@xxxxxxxxx wrote:
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

In short: I prefer simple MySQL queries and reusable PHP processing blocks. That said, MySQL may be a better/faster/more elegant solution in many applications. All depends on what you are doing. Hard to judge I think.

Maybe you can share some detail of the issues at hand ?
And of course: what do YOU feel comfortable with. Is it just a matter of striving for a neat looking result or is there money at stake ?


Rgds
SH
.



Relevant Pages

  • Coding style: PHP with Complex MySQL statements
    ... simplified, and minimal, and allow PHP to further process the received ... the difficulty is in the MySQL statements. ... you nasty-up your PHP code with many different, large SQL 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: I am totally stumped..with this on..LOAD_FILE Mysql+PHP= FSCK!!
    ... What I am trying to do is to upload files and stuff them in a mysql database. ... I copied the temporary file to somewhere else, and then handed it to MySQL..THAT WORKED.. ... Is there a way to force a close on the file..maybe that's the problem Mysql is opening a file that is not flushed to disk maybe? ... I gew the feeling its maintaining its own picture of file objects, and doesn't actually flush to the disk unless you do a copy or close php.. ...
    (comp.lang.php)
  • Re: I am totally stumped..with this on..LOAD_FILE Mysql+PHP= FSCK!!
    ... What I am trying to do is to upload files and stuff them in a mysql database. ... I copied the temporary file to somewhere else, and then handed it to MySQL..THAT WORKED.. ... Is there a way to force a close on the file..maybe that's the problem Mysql is opening a file that is not flushed to disk maybe? ... I gew the feeling its maintaining its own picture of file objects, and doesn't actually flush to the disk unless you do a copy or close php.. ...
    (comp.lang.php)
  • Re: com_dotnet
    ... And if MySQL isn't installed, the DLL won't load and phpinfowill show MySQL support isn't enabled. ... The MySQL interface is NOT compiled into PHP on the distributed Windows binaries - or you'd never be able to run PHP unless you had MySQL installed. ... *SOME* extensions are protocols, some are functional resources, and some are just type libraries. ... If you're going to compile the extension into PHP itself, the libraries must be available at compile time, and when you run PHP, or PHP won't load. ...
    (comp.lang.php)