View Full Version : include a sql queries file?

01-31-2007, 12:12 PM
Hi all,

Im a wondering...

if a store all my queries in a seperate file...and name them with logical variable names...such as...

$sql_select_all = "SELECT * FROM table";

obviously, that is just an example, and most of my queries will be multiple lines...

if i was to save that in a separate file, then include it...does that include (load the entire file in to the server memory) even if it does not use all the queries on any given page load...

i guess what i mean is, is there a way to reference that include, without loading the entire thing, and only picking out (loading) the queries i need.

what im trying to do is keep my queries out of my normal code...sorta like a stored procedure, but my hosting db is only 4.1.x

i hope this makes sense.

Thanks, Justin

01-31-2007, 07:53 PM
Yes it would load them all to memory, I'm not sure how functions work though, you could try that.

01-31-2007, 09:56 PM
Yes, functions would be parsed as you include them and they would reside in memory during the script's execution.

If you really want/need this, you could create a function like this.

function getQuery($name) {
switch ($name) {
case 'select_all':
return 'SELECT * FROM table';
return '';

The function would obviously be parsed on compile, but no global variables are created, so you don't waste the global variable space.

I'm not sure if this is the best method though.

I prefer to seperate business logic and presentation only. I have my queries in my business logic still, as they are tightly linked. There are probably better methods out there though, you just have to find them.

02-01-2007, 12:00 AM
I don't really think keeping all the queries together serves any purpose, other than making your code harder to follow. If you've got queries that are long, then you're going to struggle to come up with useful enough variable names that aren't as long as the query itself ($query_to_select_all_the_people_with_a_birthday_this_week anyone?)

Keeping your SQL (and other logic) away from your html; and keeping the raw SQL away from the more generic logic is a good idea, but grouping together unrelated things, just because they are all queries seems pointless.

What would you do with queries that rely on user input for a criteria? You could use sprintf with placeholders, but then you need to remember (or lookup) what order the placeholders are in. And which queries even have placeholders?

02-01-2007, 12:41 AM
What would you do with queries that rely on user input for a criteria?

just define the variables, then include them in the query...

//On my current page...
$name = 'Justin';

//On my sql queries page...
$qry_SelectUserByName = 'SELECT user_name FROM tbl_users WHERE user_name = $name';

also, for the naming of the query, it would not be to long,...

such as, a query that selects top 10 specific fields (i dont use star, only return useless data at the point of execution), from a table, then joins two other tables onto it...could be $qry_SelectTop10Posts...

THanks, Justin