help needed in PHP code

Discussion in 'PHP' started by Andy_ameed, Jan 23, 2010.

  1. #1
    mysql_query("UPDATE sites SET site_count =$newRow, access_time=$time, value=$value WHERE site_name='$site'") or die(mysql_error());
    
    Code (markup):
    I would like to add auto refresh to the page instead of
    die(mysql_error());
    Code (markup):
    can any of the expert guys help out..


    i tried to put this one
    <?php echo "<meta http-equiv='refresh'>" ?>
    Code (markup):
    but its coming up with an error. what i need is to refresh the page rather than letting out a die error message.
     
    Last edited: Jan 24, 2010
    Andy_ameed, Jan 23, 2010 IP
  2. hasanbasri

    hasanbasri Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try something like that

    $result=mysql_query($sql);
    $data=mysql_fetch_array($result);
    if($data) 
    your code ;
    else
    {
    $thispage=$_SERVER['php_self'];
    $location='location:' . $thispage;
    header($location);
    //header('location:$thispage');
    Code (markup):
     
    hasanbasri, Jan 24, 2010 IP
  3. K.Meier

    K.Meier Well-Known Member

    Messages:
    281
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #3
    What is the error message?

    hasanbasri solution sounds good, but beware that you can only use the
    header('location:$thispage')
    PHP:
    if you didn't echo anything else before on that page.
     
    K.Meier, Jan 24, 2010 IP
  4. Andy_ameed

    Andy_ameed Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    Guys i had tried that, but it is not working. here is the actual code

    mysql_query("UPDATE sites SET site_count =$newRow, access_time=$time, value=$value WHERE site_name='$site'") or die(mysql_error());

    The page ends up an error sometimes. but when i refresh it, then it becomes ok.

    so what i would like is to replace this part die(mysql_error()); with an auto refresh code like <?php echo "<meta http-equiv='refresh'>" ?> but the thing is its ending up with an error.

    can u guys modify this code <?php echo "<meta http-equiv='refresh'>" ?>

    so that it can be replaced instead of die(mysql_error());
     
    Last edited: Jan 24, 2010
    Andy_ameed, Jan 24, 2010 IP
  5. Andy_ameed

    Andy_ameed Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    when i used the above codes, a Parse error is shown.
     
    Andy_ameed, Jan 24, 2010 IP
  6. White Sun

    White Sun Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    function pageRefresh()
    {
    	$location = $_SERVER['PHP_SELF'];
    	header("Location: $location");
    }
    
    $query = mysql_query("SELECT * FROM table") or die(pageRefresh());
    PHP:
    Let us know whether it works the way you wanted or not.
     
    White Sun, Jan 24, 2010 IP
  7. Andy_ameed

    Andy_ameed Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    Thanks for the help. There is no header in the page. the page name is data_conect.php

    when i checked with the code given it is saying cannot find header. how should i continue?
     
    Andy_ameed, Jan 24, 2010 IP
  8. White Sun

    White Sun Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    As long as it's a PHP script, $_SERVER properties will always ( with a few exceptions ) be active.
    Can you show ( screenshot, copy/paste, .. ) us the exact output ?
     
    White Sun, Jan 24, 2010 IP
  9. domain-man

    domain-man Peon

    Messages:
    173
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    look pls this code
     
    domain-man, Jan 24, 2010 IP
  10. Andy_ameed

    Andy_ameed Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #10
    Parse error: syntax error, unexpected '<' in /home/valued/public_html/database_connect.php on line 10

    domain man this is the error i am getting. and the page does not have a header. this is a page that provides data to the main page
     
    Andy_ameed, Jan 24, 2010 IP
  11. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #11
    so you are saying, by the time this code is called the header is already outputted? if yes, then you will need to change the way its working. The example you are presenting for the refresh is php outputting HTML.
    And there's 2 files ? database_connect.php and data_connect.php ?
     
    shallowink, Jan 24, 2010 IP
  12. Andy_ameed

    Andy_ameed Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #12
    Yes ofcourse. Guys now please tell me what is wrong with this code

     
    Andy_ameed, Jan 24, 2010 IP
  13. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You should use square brackets around array subscripts, not round parentheses:

    $_SERVER['PHP_SELF']
    Code (markup):
     
    SmallPotatoes, Jan 24, 2010 IP
  14. Andy_ameed

    Andy_ameed Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #14
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

    when using those brackets the above error comes up
     
    Andy_ameed, Jan 24, 2010 IP
  15. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #15
    That error means you have an extra space in there somewhere, probably inside the square brackets.

    $_SERVER['PHP_SELF']

    with no spaces.
     
    SmallPotatoes, Jan 24, 2010 IP
  16. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #16
    function pageRefresh1()
    {
    echo '<meta http-equiv="refresh" content="3;' . $_SERVER['PHP_SELF'] .'">';
    }
    
    PHP:
    and the 3; is required to make the HTML redirect work. replace 3 with 0 if you want it immediate.

    or you can write the echo line like this :

    echo "<meta http-equiv='refresh' content=3;{$_SERVER['PHP_SELF']}>";
     
    shallowink, Jan 24, 2010 IP
  17. Andy_ameed

    Andy_ameed Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #17
    Thanks all of you guys and specially to "shallowink". Its your code that worked.
     
    Andy_ameed, Jan 24, 2010 IP