www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Really WEIRD problem with php???? php BUG ??? Please help

  1. #1
    Join Date
    Feb 2009
    Posts
    26

    Exclamation Really WEIRD problem with php???? php BUG ??? Please help

    I am trying to build a forum website with php5.2 mysql5.x I am trying to print views for perticular topic but i am stuck with a really weard problem I have written a class called forum as follows(Just kept the essential part) :
    PHP Code:
    class Forum
    {
        private 
    $login;
        private 
    $entryTime;
        public function 
    __construct($login,$entryTime)
        {    
            
    $this->login=$login;
            
    $this->entryTime=$entryTime;
        }
        public function 
    getLogin()
        {
            return 
    $this->login;
        }
        public function 
    getEntryTime()
        {
            return 
    date("Y-m-d H:i:s",$this->entryTime);
        }
        public function 
    getViews()
        {
            
    $con=new Connection();
            
    $forumSet=$con->runQuery("select views from forum where login='".$this->getLogin()."' AND entry_time='".$this->getEntryTime()."'");
            
    $views=array(mysql_fetch_array($forumSet));
            
    $views=$views[0]['views'];
            return 
    $views;
        }

    Now in file i am incrementing the views in database as follows
    PHP Code:
    1.require_once('Connection.class.php');
    2.$forumId_login=$_POST['forumId_login'];
    3.$forumId_entryTime=$_POST['forumId_entryTime'];

    4.$con=new Connection();
    5.echo "select * from forum where login='".$forumId_login."' AND entry_time='".date("Y-m-d H:i:s",$forumId_entryTime)."'";
    7.$forumSet=$con->runQuery("select * from forum where login='".$forumId_login."' AND entry_time='".date("Y-m-d H:i:s",$forumId_entryTime)."'");
    8.$forum=mysql_fetch_array($forumSet);
    9.echo "in//".$forum['views'];// Printing current database value

    10.$newForum=new Forum($forumId_login$forumId_entryTime);
    11.echo "out//".$newForum->getViews();
    12.$_views=$newForum->getViews();
    ///////////
    13.$_views=strval(intval($_views)+1);//updating database
    14.$con->runQuery("update forum SET views='".$_views."' where login='".$forumId_login."' and entry_time='".date("Y-m-d H:i:s",$forumId_entryTime)."'"); 
    Before incrementing the views i am printing the value for current database at line 9 but Magically??? i am getting value which is as if already incremented and as a result when i actually increment it it gets incremented twice ie if my database has value as 1 i get 3,
    I don't know what to do with it?? It seems that increment function gets magically called before i call it, when i remove increment function views never gets incremented, I am new to php is it a php bug?? And when i run the page in google chrome increment happens only once ?? How it is possible that server giving browser dependent code , i am currently working on localserver i localserver causing problem????
    Last edited by spsarolkar; 02-14-2009 at 03:57 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    In the getviews method, shouldn't this...
    PHP Code:
    $views=$views[0]['views'];
    return 
    $views
    ...just be this...
    PHP Code:
    return $views['views']; 
    ...?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Urgh...the more I look at that, the odder it looks. How about:
    PHP Code:
        public function getViews()
        {
            
    $con=new Connection();
            
    $forumSet=$con->runQuery("select views from forum where login='".$this->getLogin()."' AND entry_time='".$this->getEntryTime()."'");
            
    $row mysql_fetch_assoc($forumSet);
            return 
    $row['views'];
        } 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Feb 2009
    Posts
    26
    I had the same code previously but the same effect , but problem is why increment is happening before calling the function ??

  5. #5
    Join Date
    Feb 2009
    Posts
    26

    Now i have reduced more still two increments

    PHP Code:
    require_once('Connection.class.php');
    $forumId_login=$_POST['forumId_login'];
    $forumId_entryTime=date("Y-m-d H:i:s",$_POST['forumId_entryTime']);

    $con=new Connection();
    $forumSet=$con->runQuery("select * from forum where login='".$forumId_login."' AND entry_time='".$forumId_entryTime."'");
    $forum=mysql_fetch_array($forumSet);
    $_views=$forum['views'];//I am getting already incremented value ??? How it is possible
    ///////////
    $_views=strval(intval($_views)+1);//updating database
    $con->runQuery("update forum SET views='".$_views."' where login='".$forumId_login."' and entry_time='".$forumId_entryTime."'"); 
    and the thing is chrome increment properly how is it possible?? Is it a localserver problem??
    Last edited by spsarolkar; 02-14-2009 at 04:36 AM.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    I don't see anything in the code you showed us that would cause the symptoms you describe. All I can suggest at this point is to break things down into small parts and test them incrementally, making sure each small part (e.g. method) does what you expect and intend it to do.

    BTW, I'd recommend that the update query also be incorporated into your class as another method to help clean things up in the main code.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    You don't by any chance have a MySQL trigger defined for that table that increments the value on an update, do you?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Feb 2009
    Posts
    26
    no i am not using any triggers ,
    how come two browsers giving two different functionalities for php code
    i think for time being i will assume it as localserver problem and proceed, i will see how actual server responds to it after deploying

  9. #9
    Join Date
    Feb 2009
    Posts
    26

    Question

    I just figured out that its showing problem when i refresh the page with the topic in , but when i go back in topics list it shows correct views?? also when i swich ie8 compatibility views it show me correct values (only one increment)
    Last edited by spsarolkar; 02-14-2009 at 06:26 AM.

  10. #10
    Join Date
    Nov 2008
    Posts
    2,477
    Testing with IE... there's your problem

  11. #11
    Join Date
    Jan 2009
    Posts
    3,346
    It sounds like your problem may not be with your PHP code at all. Are you using any Javascript validation or creative html form usage? If browsers are causing different behavior it is usually something in the html or Javascript that is causing the troubles.

  12. #12
    Join Date
    Feb 2009
    Posts
    26

    Arrow

    I don't know but in ie8 when i toggle compatibility modes as page gets reloaded automatically at that time it shows only one increment but when i refresh the page it shows two increments ,
    another thing is i had designed views previously on other localserver with different page, it was giving me proper results at that time i just checked that page with this server but it is giving same problem, i am using 'wampserver' and maybe it is causing problem, i don't know what is the reason for it, it is also giving problems on firefox but NOT on chrome???
    I am developing a fully ajax website, i am using RSH0.6(really simple history) for storing the history state, my every request is going through javascript but that should not cause problem like this???
    I hope it will not cause problem after deploying it

  13. #13
    Join Date
    Jan 2009
    Posts
    3,346
    Does your PHP return the correct results when you are not using AJAX/Javascript calls? This is a way to know for certain whether it is php or not. After that I would start stripping down the code to its minimum to track down the issue. It sounds like it might be in the response code handling or during a timed event but that is just a guess.

  14. #14
    Join Date
    Feb 2009
    Posts
    26
    Responce is two increments when i checked dividing the code into smaller parts its results are as follows

    PHP Code:
    require_once('Connection.class.php');
    $forumId_login=$_POST['forumId_login'];
    $forumId_entryTime=date("Y-m-d H:i:s",$_POST['forumId_entryTime']);

    $con=new Connection();
    $forumSet=$con->runQuery("select * from forum where login='".$forumId_login."' AND entry_time='".$forumId_entryTime."'");
    $forum=mysql_fetch_array($forumSet); 
    Ok uptil now
    problem area
    PHP Code:
    $_views=$forum['views'];//I am getting already incremented value ??? How it is possible
    echo $_views
    now if from mysql consol i am getting the value as 10
    but running the same query in 'php having increment function' i get value with one increments which is again incremented by increment function which results in two increments, when i remove increment function i get zero increments???, it seems that the increment function gets called somehow at start.

  15. #15
    Join Date
    Jan 2009
    Posts
    3,346
    Can you post the code including the 'increment function'? The code you posted looks fine.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles