SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Variable naming convention for multiple SQL-queries: best practice?

  1. #1
    SitePoint Evangelist spinmaster's Avatar
    Join Date
    Mar 2005
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable naming convention for multiple SQL-queries: best practice?

    Hi,

    I have certain functions where I need to execute multiple SQL-queries right
    after each other within a single function (INSERT, SELECT, UPDATE).

    I'd like to hear the best coding practice in terms of variable names
    for the queries. I want to avoid something like this:

    PHP Code:
    function doSomething() {

    // insert query
    $sql1 "INSERT something";
    $result1 $db->query($sql1);

    // select query
    $sql2 "SELECT something"
    $result2 $db->query($sql2);

    // update query
    $sql3 "UPDATE something";
    $result3 $db->query($sql3)

    Giving each variable inside the function a name like "$sqlInsertUser" and "$resultInsertUser" makes the code also not really readable...

    someone who can give some input??

  2. #2
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    2,999
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To tell the truth I would go with

    PHP Code:
    function doSomething() {

    // insert query
    $sql "INSERT something";
    $result $db->query($sql);

    // select query
    $sql "SELECT something";
    $result $db->query($sql);

    // update query
    $sql "UPDATE something";
    $result $db->query($sql)

    -- Jelena --

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what if he needs to use a query multiple times?

    i prefer to use memorable things:
    $update_sql
    $userupdatesql
    etc

    So i know im using the right one (i dont see how it makes it look ugly)

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spinmaster
    Giving each variable inside the function a name like "$sqlInsertUser" and "$resultInsertUser" makes the code also not really readable...
    What's wrong with $sqlInsertUser ? I'd use that.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $result1 $db->query("INSERT something");
    $result2 $db->query("SELECT something");
    $result3 $db->query("UPDATE something"

  6. #6
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On another note, you don't really need to be assigning $result variables for INSERT/UPDATE queries since they won't be returning anything anyways.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  7. #7
    SitePoint Evangelist spinmaster's Avatar
    Join Date
    Mar 2005
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx for the replies...

    Quote Originally Posted by Toly
    On another note, you don't really need to be assigning $result variables for INSERT/UPDATE queries since they won't be returning anything anyways.
    Normally, you're right... but I'm using PEAR-DB and the $result variable is useful to check if the query was successfull:

    PHP Code:
    $result $db->query("INSERT INTO table VALUES ($id, sitepoint, forum)");
    if (
    DB::isError($result)) {
        die(
    $result->getMessage());


  8. #8
    SitePoint Addict Hero Doug's Avatar
    Join Date
    Nov 2003
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use arrays myself.

    Code:
    function doSomething() { 
    
    // insert query 
    $sql['insert'] = "INSERT something"; 
    $result['insert'] = $db->query($sql); 
    
    // select query 
    $sql['select'] = "SELECT something"; 
    $result['select'] = $db->query($sql); 
    
    // update query 
    $sql['update'] = "UPDATE something"; 
    $result['update'] = $db->query($sql) 
    }
    That way if you have multiple queries on one page it makes it easy to remember what queries are for what purpose. You can essentially use the same name for them but still give them distinct unqiueness.

  9. #9
    SitePoint Evangelist spinmaster's Avatar
    Join Date
    Mar 2005
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hero Doug
    I use arrays myself.

    Code:
    function doSomething() { 
    
    // insert query 
    $sql['insert'] = "INSERT something"; 
    $result['insert'] = $db->query($sql); 
    
    // select query 
    $sql['select'] = "SELECT something"; 
    $result['select'] = $db->query($sql); 
    
    // update query 
    $sql['update'] = "UPDATE something"; 
    $result['update'] = $db->query($sql) 
    }
    Hey, for me that seems to be the best solution! Thx for that hint!

  10. #10
    SitePoint Addict Hero Doug's Avatar
    Join Date
    Nov 2003
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    glad to help

Bookmarks

Posting Permissions

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