SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $result and $sql multiple times in a script

    Hi,

    I'm writing some scripts that make multiple queries on the database.

    I use temporary variables to contain the sql code ($sql) and the result ($result).

    Sometimes I will check the $result is set in an if statement. If I have already used $result it will already be set making this a bad way of checking if results have been pulled out of the database.

    So.. I've been going through the scripts renaming $sql as $sql1, $sql2 etc. However, with various includes which contain these variables it becomes increasingly difficult and a pain.

    My question - anyone have a better way of doing this? Do most people just use $sql and $result multiple times and use a different way of checking that mysql query works.

    Cheers,

    Chris

  2. #2
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I get into these situations I will dump the results into an array to use later and then assign the new result set into the results variable.

    I'm just one of those that REALLY hates that results1, results2, etc etc ... it's really hard to keep track of.

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I try to summarize what the query does:
    PHP Code:
    $customerlookup_select
    $customerlookup_result
    $customerlookup_fetch 
    It helps me keep it organized in my mind and my code. I try to always avoid using generic variables -- overwriting a variable on accident can really suck
    MySQL v5.1.58
    PHP v5.3.6

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I personally use $rs for stuff that I know will not be useful again, like if I am going to use the resource immediately following the query or I know its a simple script that won't have other queries.

    Otherwise I will name them to describe what it does, if i pull out of a member table, I might name it $memberRS

  5. #5
    SitePoint Addict tbakerisageek's Avatar
    Join Date
    Sep 2006
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BrandonK View Post
    I try to summarize what the query does:
    PHP Code:
    $customerlookup_select
    $customerlookup_result
    $customerlookup_fetch 
    It helps me keep it organized in my mind and my code. I try to always avoid using generic variables -- overwriting a variable on accident can really suck
    This helps me as well. I even go a step further and make sure to name things that occur outside the scope of the "Main content" but still on each page (Navigation stuff, Informational panes on the right side of the window...) with an appropriate prefix so there is a minum of a chance that I will reuse the variable

    Code:
    $pollQuestionQuery
    $pollQuestionResult
    $pollQuestionNumResults
    $pollQuestionInsertID
    $pollQuestionRow
    making sure to use the same prefix for each set of related variables.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the answers. It's interesting that most of the textbooks I use simple just use $sql, $result, $row, etc multiple times.

    I think I'll start using a unique variable for each query - it makes more sense to me.

    Cheers,

    Chris


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
  •