if select fails redirect to another apge

Discussion in 'PHP' started by ferdousx, Dec 26, 2008.

  1. #1
    Hello. Is it possible to use a select statement as if it dont fetch any row then it will be redirect to some other page?
    For expamle a mysql statement:
    mysql_query("select somecolumn from sometable where someid='$id' ") or die ("no $id is found in someid field");
    this statement will show the die msg when it dont found any match. But I dont want to use die rather redirect to some other page in such cases (where 'where clause' dont match).
    Can it be done?
    Thanks.
     
    ferdousx, Dec 26, 2008 IP
  2. Goodlookinguy

    Goodlookinguy Peon

    Messages:
    5
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $query = mysql_query("select somecolumn from sometable where someid='$id'");
    if (!$query) {
    header("Location: file.php?do=mainpage");
    exit();
    }
    else {
    // Blah
    }

    That is one way, the other way is to

    $query = mysql_query("select somecolumn from sometable where someid='$id'");
    if (!$query) {
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://site.com\" />";
    exit();
    }
    else {
    // Blah
    }
     
    Goodlookinguy, Dec 26, 2008 IP
    ferdousx likes this.
  3. ferdousx

    ferdousx Peon

    Messages:
    168
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Oops. I should have try the first one.
    thanks.
     
    ferdousx, Dec 26, 2008 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    This also works.. I think.

    
    mysql_query("select somecolumn from sometable where someid='$id' ") or header("Location: file.php?do=mainpage");
    
    PHP:
     
    Kaizoku, Dec 26, 2008 IP
  5. ferdousx

    ferdousx Peon

    Messages:
    168
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Having difficulties using header directly with mysql_query. But solved it by using if(mysql_num_rows($query)==0){header("....");}. Thanks anyway.
     
    ferdousx, Dec 27, 2008 IP