SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check if variable exists....

    I'm working with a dynamic site using PHP and MySQL (what else would I use? ). Say, for example, I have a page named about.php.

    And depending upon what an id variable is when passed to the page (i.e. http://host.com/about.php?id=x), it displays the content of the database where the id=x.

    But, if no ID is passed to the page (i.e. http://host.com/about.php), I need to assign a value to $id.

    So how would I check if the variable exists? That way if it does, the content will be displayed, and if it doesn't, a value wil be assigned to it and that content will be displayed.
    PHP Code:
    if ($id == '')
    {
    $id="x";
    }
    // and
    if ($id == "")
    {
    $id="x";

    Both of those don't work. As always, thanks for your help.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  2. #2
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try the isset() function:

    PHP Code:
    if(!isset($id))
    {
    $id "1";


  3. #3
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That doesn't work either.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  4. #4
    Not Bad, eh? Justin Sampson's Avatar
    Join Date
    Aug 2000
    Location
    N.S., Canada
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know why that wouldn't work, and I don't see why your 2 examples wouldn't work either.

    Are you sure it's not somthing else?

  5. #5
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try
    if(empty($id))

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you maybe have register_globals off in php.ini?
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  7. #7
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That didn't work either. It seems like all of those examples should work!
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  8. #8
    :) delemtri's Avatar
    Join Date
    Jun 2001
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:   if (!$id) { $id=1; }

  9. #9
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, something's up, because ALL of the below should work:

    PHP Code:
    if (!$id
    PHP Code:
    if (!isset($id)) 
    PHP Code:
    if (empty($id)) 
    If those aren't working, then something else is going on...perhaps the id value is coming from somewhere else, like a cookie? If you're look for the id value in the querystring (and only from the querystring), replace $id with $HTTP_GET_VARS["id"], to ensure that you're looking at the exact variable that you want.

    If that doesn't work, then something might be up with your host, because it ought to work.

  10. #10
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We appear to be 0 for 5 now.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  11. #11
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the code if it helps:
    PHP Code:
    <?php
    // check to see if $id contains a value
    if (!$id)
    {
        
    $id="2";
    }
    else
    {
        
    // connect to the main content database
        
    $maincnx = @mysql_connect("localhost","root");
        if (!
    $maincnx)
        {
            echo(
    "<p>There was an error in connecting to the database: " mysql_error() . "<br />\n" .
            
    "A message has been sent to the webmaster.  Please try again later.</p>");
            exit();
        }
        
    // select the appropriate database
        
    $mainselect = @mysql_select_db("main",$maincnx);
        if (!
    $mainselect)
        {
            echo(
    "<p>There was an error in selecting the database: " mysql_error() . "<br />\n" .
            
    "A message has been sent to the webmaster.  Please try again later.</p>");
            exit();
        }
        
    // extract the content and title from the database
        
    $content = @mysql_query("select id, content, title, date from maincontent where id='$id'");
        if (!
    $content)
        {
            echo(
    "<p>There was an error in displaying the content for this page: " mysql_error() . "<br />\n" .
            
    "A message has been sent to the webmaster.  Please try again later.</p>");
            exit();
        }
        
    $results mysql_fetch_array($content);
        
    $contentid $results["id"];
        
    $contenttitle $results["title"];
        
    $contenttext $results["content"];
        
    $contentdate $results["date"];
        
    // assign the values to a temporary array, reassign to variables, and display on the page
        // site template
        
    include("header.php");
        include(
    "toppage.php");
        echo(
    "$contenttext");
        include(
    "footer.php");
    }
    ?>
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  12. #12
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If those outside problems do not exist (IE: if you're using $HTTP_GET_VARS[] or if you're sure that the variable isn't being set through a form, cookie, or session variable of some sort), then something's up with the PHP installation/webhost.

  13. #13
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll inquire with my host. Thanks.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  14. #14
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I would like to apologize. I just realized something. If $id isn't set it sets it to 2 and then forgoes the rest of the script because I have satisfied the if clause and so it skips the else clause.

    I'm so stupid!!!!!!
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  15. #15
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    D'oh! I only glanced over your large block of code, so I missed it to. Whoops! Hehe. No problem, glad it's working for you now.


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
  •