tags:

views:

47

answers:

5

Hi, I'm just learning PHP and I'm trying to figure out how I could store my database queries in a separate file either as part of a class, method or just as simple variables.

For example: I have a query that looks like this:

$checkuser = mysql_query("SELECT UserName, Email FROM users 
                            WHERE UserName = '$username4db' OR EMAIL = '$email4db'") 
             or die($checkuser_error);

Instead of writing it inline within the file, I'd like to move this to a central place and then use them from there.

I tried using an include file and simply calling the variable in place but it doesn't work.

Can some one help please? Thanks!

+1  A: 

Storing the queries at a central place makes hardly sense IMO, as they will always be part of the program logic, and you will need to see them (and alter them) when you work on the program's flow.

You could put something together with queries containing placeholders that you then fill in using sprintf(), but I don't really see the point.

Pekka
@Pekka, Really? You don't see the point of keeping data access code separate? For years I've been told that doing this is a bad idea because maintenance becomes a nightmare. However, I realize that PHP is a whole different world. I'm still learning the methodologies behind it. I am curious though as to what other suggestion I will get. Thanks for the suggestion!
Scott W.
@Scott data access code is one thing and program logic is another.
Col. Shrapnel
@Col, Exactly, so what's your point?
Scott W.
@Scott interesting, I've never heard of the concept of separating queries like that. If you add a field to a table for example, you will have to modify both the location where the query is executed (for output or whatever), and locate the (separate) query and change that as well. I don't see how this can ever be better for maintenance. This can be a design paradigm I don't know about but I for one have never heard of it.
Pekka
A: 

You could simply store the queries as strings in a separate file, and then call those string variables when you want to use the query. However, I'm guessing most of your queries will require arguments - in which case you would need to create a series of functions (with or without a class) to build the queries with the appropriate variables.

DiglettPotato
@diglett, Yes, thank you! This is exactly what I ended up doing.
Scott W.
A: 

I find the "one query file" paradigm very interesting. I think it makes more sense to just keep the queries where you want to get the data because that is where you expect to find it. Instead, others thing it is a good idea to put all of the queries in a central location that you can always refer back to, even when it gets to 5000 lines. I can't say that there is a right answer.

You could assign a function per query instead. Just have a file that includes all the queries you want each defined in functions. So when you want some specific query from your included file you just run get_random_data_query_uno() or what have you.

tandu
@tandu, Sorry I wasn't specific enough. By central file I meant central file per directory. For example, all security related controls like login, registration, account activation etc. are contained in their own directory. The number of data access code per functionality is less than a hadndful, yet very easy to find and edit them as required.
Scott W.
@tandu, I did end up creating a class with methods per query. I had to move all the logic along and it works fine. Thanks for the advice!
Scott W.
A: 

You should look at the MVC design pattern, the whole purpose is to provide a clean way of organizing code based on function.

The models are where your database interaction code goes. Depending on the MVC implementation this is sometimes a direct representation of the database table, or it could be a collection of functions you write yourself to deal with the problem at hand.

The views are for display code. As a general rule when writing views only HTML and basic PHP (echoing variables, a foreach loop to build a table) should go in the view.

The controller is where the program logic is placed. From the controller you call models to do the data processing necessary to build the data that is then passed into the view.

That's a very basic explanation, and always wikipedia has more to say on the topic: http://en.wikipedia.org/wiki/Model-view-controller.

There are many MVC frameworks out there in PHP that you can use to have a good starting point for building sites. I use and recommend CodeIgniter as it's very easy to use and flexible in its implementation.

bradym
@bradym, Thank you! This is good advice. I have been playing with asp.net MVC but did not know how similar different it would be in PHP. PHP has come a very long way and so far I love it so much more than .net.
Scott W.
A: 

Your question has both sense and nonsense.
You can use some ORM to encapsulate a query into some class. It will make sense.
But storing raw queries has no sense. Especially in a form you posted. All queries will be run unconditionally, no matter do you need this particular data, or not.
At lest encapsulate it in a function call

function checkuser($username4db, $email4db) {
  $sql = "SELECT UserName, Email FROM users WHERE UserName = %s OR EMAIL = %s";
  return db::getrow($sql, $username4db, $email4db); 
}
  • in this case the query would be run only when checkuser() function is called.

but I still see no sense in storing all site queries in one file. Why not to store it logically, based on what site part this query belongs to?

Col. Shrapnel
@Col, Sorry but all queries don't run unconditionally. Did you read my post? I wrote class, function, variable. Even putting it into a constant would make it run only if called.
Scott W.