Closed Thread
Results 1 to 7 of 7

Thread: query string and isset

  1. #1
    jonnyfolk is offline Newbie
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    query string and isset

    Hi,

    I normally program in perl and am trying to translate one of my scripts to php.

    The following snippet doesn't set the variable and I thought it would!
    What the script does is to redirect to a different script depending on whether a query exists, and depending on the existence of a parameter if there is a query string.

    I would like to know why, when calling without a query $scriptrl is not being set, and also how do I query a parameter from a GET call (equivalent of CGI
    Code:
    $var = param('variable');
    Thanks,
    Jon

    Code:
    $query = $_SERVER["QUERY_STRING"];
    
    if(! isset($query))
    {
    $scriptrl = 'g_search.php';
    }
    Last edited by jonnyfolk; 03-03-2010 at 02:17 PM. Reason: changed script name for privacy reasons - no impact on thread!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    webcodez's Avatar
    webcodez is offline Programmer
    Join Date
    Feb 2010
    Posts
    148
    Rep Power
    0

    Re: query string and isset

    EDIT: nvm.

    Try:

    Code:
    if(!isset($_SERVER['QUERY_STRING'])) 
    or to check if it's empty:

    Code:
    if(empty($_SERVER['QUERY_STRING'])) 
    Not sure if this is what you mean?

    As your script's if loop will always return true as before the loop you set the variable $query ( and the function isset is independent of whether a variable is empty or not, just cecks if it's set - it's set ).

  4. #3
    jonnyfolk is offline Newbie
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Re: query string and isset

    I see - I hadn't realised that consequence. I chose the 'empty' option and it worked perfectly. Thanks.

    Regarding the param from the GET call, eg script.php?var=one&var1=two&var2=three how to I get the var1 value?

  5. #4
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: query string and isset

    isset and empty is a little special and can not always be trusted, as some can be true even if they have the value 0 (zero). The safest way is actually to check with if ($var == "").

    you get the var1 with $_GET['var1']
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  6. #5
    webcodez's Avatar
    webcodez is offline Programmer
    Join Date
    Feb 2010
    Posts
    148
    Rep Power
    0

    Re: query string and isset

    Glad to hear that it's working now

    @Orjan True, thanks for heading that up, I experienced that problem with the value '0' a while ago indeed, forgot about that but I think that was with isset function.
    Not sure if this would be a problem in this case though.

    And the GET var1 can indeed be accesed within the GET array with as sub-variable name 'var1', as in general it's:

    Code:
    $_GET['sub_variable_name'
    So when adding a variable through url called 'var1' it automaticly is converted into a GET variable $_GET['var1'].

    Cheers.

  7. #6
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: query string and isset


  8. #7
    jonnyfolk is offline Newbie
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Re: query string and isset

    Thank you for your input. I appreciate the responses and will consider the information I've been given. A very interesting (and successful) first day on the boards!!

    Jon

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. 1 query vs multiple query
    By Vaielab in forum Database & Database Programming
    Replies: 1
    Last Post: 08-30-2011, 12:16 PM
  2. isset function does not work
    By thatsme in forum PHP Development
    Replies: 3
    Last Post: 07-05-2011, 09:02 PM
  3. converting object to a string during database query
    By mutago in forum PHP Development
    Replies: 3
    Last Post: 05-22-2011, 01:20 PM
  4. Query as to how string to ascii conversion works?
    By squid attack in forum C and C++
    Replies: 8
    Last Post: 09-21-2010, 03:04 PM
  5. adding int to string,creat a file named string..
    By emda321 in forum C and C++
    Replies: 5
    Last Post: 09-10-2009, 07:15 AM

Tags for this Thread

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