/    Sign up×
Community /Pin to ProfileBookmark

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) :

[code=php]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;
}
}[/code]

Now in file i am incrementing the views in database as follows

[code=php]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).”‘”);[/code]

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????

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 14.2009 — In the getviews method, shouldn't this...
[code=php]
$views=$views[0]['views'];
return $views;
[/code]
...just be this...
[code=php]
return $views['views'];
[/code]
...?
Copy linkTweet thisAlerts:
@NogDogFeb 14.2009 — Urgh...the more I look at that, the odder it looks. How about:
[code=php]
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'];
}
[/code]
Copy linkTweet thisAlerts:
@spsarolkarauthorFeb 14.2009 — I had the same code previously but the same effect , but problem is why increment is happening before calling the function ??
Copy linkTweet thisAlerts:
@spsarolkarauthorFeb 14.2009 — [code=php]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."'");[/code]
and the thing is chrome increment properly how is it possible?? Is it a localserver problem??
Copy linkTweet thisAlerts:
@NogDogFeb 14.2009 — 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.
Copy linkTweet thisAlerts:
@NogDogFeb 14.2009 — You don't by any chance have a MySQL trigger defined for that table that increments the value on an update, do you?
Copy linkTweet thisAlerts:
@spsarolkarauthorFeb 14.2009 — 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
Copy linkTweet thisAlerts:
@spsarolkarauthorFeb 14.2009 — 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)
Copy linkTweet thisAlerts:
@MindzaiFeb 14.2009 — Testing with IE... there's your problem :p
Copy linkTweet thisAlerts:
@criterion9Feb 14.2009 — 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.
Copy linkTweet thisAlerts:
@spsarolkarauthorFeb 15.2009 — 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
Copy linkTweet thisAlerts:
@criterion9Feb 15.2009 — 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.
Copy linkTweet thisAlerts:
@spsarolkarauthorFeb 18.2009 — Responce is two increments when i checked dividing the code into smaller parts its results are as follows

[code=php]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);[/code]

Ok uptil now

problem area
[code=php]$_views=$forum['views'];//I am getting already incremented value ??? How it is possible
echo $_views;[/code]

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.
Copy linkTweet thisAlerts:
@criterion9Feb 18.2009 — Can you post the code including the 'increment function'? The code you posted looks fine.
Copy linkTweet thisAlerts:
@spsarolkarauthorAug 01.2009 — [B][U]Problem was new behavior of ie8 for hash change ...[/U][/B]

Problem is that internet explorer 8 now adds history event for hash change therefore when i was using internet explorer 8 i was getting two increments, It is all because the way RSH0.6 is designed, as previous versions of internet explorer did not support addition of history event for hash changes RSH0.6 has designed to inject history event via iframe. Thus now there are two requests going one for iframe and one when hash change occures. for some reason internet explorer ignored one alert message which i put for displaying received data as difference between two requests is very minute so it showed me only output of last request that was made OR it ignored first request response as immediately second request is send

That solved my problem. I didn't made changes yet but now i know what is the problem

Thank you for everybody's help
×

Success!

Help @spsarolkar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...