Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts

    include a sql queries file?

    Hi all,

    Im a wondering...

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

    PHP Code:
    $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

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Yes it would load them all to memory, I'm not sure how functions work though, you could try that.

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

    PHP Code:
    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.

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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?
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GJay View Post
    What would you do with queries that rely on user input for a criteria?
    just define the variables, then include them in the query...
    PHP Code:
    //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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •