homepage Welcome to WebmasterWorld Guest from 54.205.254.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP function processes twice?
Simple math comes out wrong!
NogginAnimations




msg:3608578
 3:55 pm on Mar 23, 2008 (gmt 0)

For some reason whenever I use this code (as a hit counter), it adds two views instead of one to the database!
The code is located at the top of the page in every page.
<?php
$resultVs = mysql_query("SELECT views FROM page LIMIT 1") or die(mysql_error());
while($rVs = mysql_fetch_array($resultVs)) { $viws = $rVs['views']; } $viws = ($viws+1);
mysql_query("UPDATE page SET views='$viws'") or die(mysql_error());
?>

Why is it adding two?

 

Scally_Ally




msg:3608615
 4:44 pm on Mar 23, 2008 (gmt 0)

It doesnt look like it should be doing.
There is a simpler way though - try this

<?php
mysql_query("UPDATE page SET views=(views+1)") or die(mysql_error());
?>

This will update all page records in the table though - i dont know how your system is setup but you might want to specify which record you want to update eg.

<?php
mysql_query("UPDATE page SET views=(views+1) WHERE pageName = 'homepage'") or die(mysql_error());
?>

Hope this helps

Ally

NogginAnimations




msg:3608623
 4:53 pm on Mar 23, 2008 (gmt 0)

Thanks for your reply, the code is cleaner now, but it still adds two instead of one, I have no idea what I'm doing wrong!

coopster




msg:3609109
 1:31 pm on Mar 24, 2008 (gmt 0)

Is this contained in a function or something? A loop perhaps? Print out the query right before you run it each time to see if you are calling it more than once during script execution.

whoisgregg




msg:3609148
 2:16 pm on Mar 24, 2008 (gmt 0)

Silly questions... Are you running any kind of browser plugin/toolbar that might cause an additional hit to any page you look at? Or does the page itself have any included code that might cause a robot to visit the page?

penders




msg:3609331
 6:17 pm on Mar 24, 2008 (gmt 0)

Are you including the page in another page? Do you capture the page in the output buffer? Either way, it looks like the page is being processed twice!?

NogginAnimations




msg:3614819
 5:27 pm on Mar 30, 2008 (gmt 0)

Fixed it guys! :D
Because I'm using a URL request script to include files if the URL has a specific value added to it, the index page acts like this (for no apparent reason)

1) Process index page
2) Check for URL request
3) If URL request, Run Index page
4) If URL request, Run Index & Included pages
5) If no URL request, Run 404 page

I worked this out by telling the page to write to "INDEX_DUMP.LOG"
I used a custom log format, so I could understand the output
This is what I got;


// LANGUAGE: "Simplified Jargon"
//// AUTHOR: "Ryan (Noggin) Boylett"
/// VERSION: "3.0.1"
///// TITLE: "INDEX_DUMP.LOG"
== RUN INDEX PAGE
== PHP CODE INITIALIZED ENTITLED "?"
== PHP CODE IF STATEMENT
== PHP CODE IFTRUE REDIRECT PAGE TO \nINDEX
== PHP CODE IFTRUE PROCESS PAGE INDEX
== PHP CODE IFTRUE PROCESS PAGE INDEX&&'res/inc/home.php'
== PHP CODE IFFALSE PROCESS PAGE INDEX&&'res/inc/404.php'

So, I just added the MySQL code to every include page (which appears to only process once), and it works!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved