Forum Moderators: coopster
Doing a bunch of includes will add to the load and only for the look benefit will shorten the page.
My scripts are already quite commented
What I need is to write them in a way I could reuse many queries as if the were .tpl
Stored procedure comes to mind
Anything else?
Alternately, you could look into a framework, such as CodeIgniter or CakePHP--while neither is suitable for every purpose, I've had a tremendous amount of success developing complete websites with CodeIgniter.
Converting existing, non-OOP code to either framework, however, will be a very painful process. I would recommend simply converting your code to OOP to start with, obeying the best practices available (I.E. name your classes and functions appropriately, don't try to cram multiple objects' functionality into one object, don't make functions too long, etc.).
1. If you have MySQL 5.x, or MSSQL, you have access to both stored procedures and views.
Views will allow you to make faster, easier, simpler queries based on joined tables, hardcoded data, combined fields, etc.--in other words, if you find yourself constantly repeating a query to retrieve data from the users table with an inner join to payments, just create a view that combines the two tables and query directly from the view--it works just like querying from a table, but is much faster than running the full complex query with join every time you need to look at that information. This speed increase is generally because MySQL/MSSQL will "pre-compile" a view and cache its results as it would a normal table--for all intents and purposes in your code, the view is exactly the same as a normal table, but you get the advantage of having it in readily-accessible form without the need to manipulate the data every time you query it. I've used views to great success before in MSSQL, and MySQL's implementation isn't far behind in quality.
Stored procedures are also a good practice, because they allow you to assign a name to a specific query. If the query changes due to an underlying data structure change, your code will still work provided the same fields are provided by the query. Again, they're pre-compiled and will thus execute faster than an in-code query.
2. If you're running MySQL 4.x, or another DB that doesn't support views/stored procedures, use PHP "templates" instead. Use an object with a variable of $query, containing a method to include() a named file (queries\my-query.sql) containing each query you'll use: "<?php $this->query = 'SELECT * FROM table WHERE username = @user' ?>" or similar. You can then access $object->query and execute it. This certainly isn't the only way, or even a way I would recommend--if you have MySQL 4.x, I would highly recommend simply upgrading to 5.x. However, if you absolutely must use something else, this or a similar include system, with named files that contain parameterized SQL queries, would at least let you mimic a stored procedure.
Hope this helps!
There is a good book named "Code Complete" entirely dedicated to the topic how to make your code more readable. For a pity, way too dick :-)
Also to reuse queries, google for "Data Access Objects design pattern", where all your queries are stored inside specific per-table classes.