Weired MySQL result in php.

  • hgun77
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jun 29, 2005
  • Posts: 158
  • Loc: Malaysia
  • Status: Offline

Post March 27th, 2006, 7:09 pm

Code: [ Download ] [ Select ]
<?php
...


$link = @mysql_connect("localhost", "xx", "xx");

@mysql_select_db("xx",$link);                        
[color=red]@mysql_query("INSERT INTO Detail_Home VALUES ($t_stamp ,'$ip')",$link); [/color]
[color=red]@mysql_query("DELETE FROM Detail_Home WHERE timestamp < $timeout",$link);[/color]

$result = @mysql_query("SELECT count(DISTINCT ip) FROM Detail_Home",$link); 

$total_user = @mysql_fetch_row($result);
[color=red]@mysql_query( "INSERT INTO Summary_Home VALUES ( now(),$total_user[0])",$link);[/color]

@mysql_close($link);

?>
  1. <?php
  2. ...
  3. $link = @mysql_connect("localhost", "xx", "xx");
  4. @mysql_select_db("xx",$link);                        
  5. [color=red]@mysql_query("INSERT INTO Detail_Home VALUES ($t_stamp ,'$ip')",$link); [/color]
  6. [color=red]@mysql_query("DELETE FROM Detail_Home WHERE timestamp < $timeout",$link);[/color]
  7. $result = @mysql_query("SELECT count(DISTINCT ip) FROM Detail_Home",$link); 
  8. $total_user = @mysql_fetch_row($result);
  9. [color=red]@mysql_query( "INSERT INTO Summary_Home VALUES ( now(),$total_user[0])",$link);[/color]
  10. @mysql_close($link);
  11. ?>


I wrote a simple php script to keep track total hit on a page. And i put it on top of a page. I found that when i refresh once it run twice. After a refresh, it insert 2 record for me. Anybody know the reason? Thanks for help.
Malaysia > KL > TAR College
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post March 27th, 2006, 7:09 pm

  • hgun77
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jun 29, 2005
  • Posts: 158
  • Loc: Malaysia
  • Status: Offline

Post March 28th, 2006, 1:55 am

The code is not in any loop( i have try to add alert("test"); ) to test it. Anybody know what can cause it run twice in a hit?
Malaysia > KL > TAR College
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jun 17, 2005
  • Posts: 515
  • Status: Offline

Post March 28th, 2006, 12:35 pm

Do you mean when you first go to the page, it runs it once, but when you refresh, it runs it 2 more times?
  • hgun77
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jun 29, 2005
  • Posts: 158
  • Loc: Malaysia
  • Status: Offline

Post March 28th, 2006, 2:05 pm

nop, anytime. no matter it is first time or refresh. I have include a Iframe inside the page. izzit that's problem? or... I really cannot think about the cause of this...

Or anybody help me look at my html souce generate by php in this page :
http://www.energy-tips.com/index.php

And the only thing difference with html inside is I add this php code inside
Code: [ Download ] [ Select ]
<?php

$t_stamp = time();                                              
$timeout = $t_stamp - 120;
$ip = $_SERVER['REMOTE_ADDR'];


$link = @mysql_connect("localhost", "xx", "xx");
@mysql_select_db("xx",$link);                        
@mysql_query("INSERT INTO Detail_Home VALUES ($t_stamp ,'$ip')",$link);
@mysql_query("DELETE FROM Detail_Home WHERE timestamp < $timeout",$link);
$result = @mysql_query("SELECT count(DISTINCT ip) FROM Detail_Home",$link); 
$total_user = @mysql_fetch_row($result);
@mysql_query( "INSERT INTO Summary_Home VALUES ( now(),$total_user[0])",$link);
@mysql_close($link);

?>
  1. <?php
  2. $t_stamp = time();                                              
  3. $timeout = $t_stamp - 120;
  4. $ip = $_SERVER['REMOTE_ADDR'];
  5. $link = @mysql_connect("localhost", "xx", "xx");
  6. @mysql_select_db("xx",$link);                        
  7. @mysql_query("INSERT INTO Detail_Home VALUES ($t_stamp ,'$ip')",$link);
  8. @mysql_query("DELETE FROM Detail_Home WHERE timestamp < $timeout",$link);
  9. $result = @mysql_query("SELECT count(DISTINCT ip) FROM Detail_Home",$link); 
  10. $total_user = @mysql_fetch_row($result);
  11. @mysql_query( "INSERT INTO Summary_Home VALUES ( now(),$total_user[0])",$link);
  12. @mysql_close($link);
  13. ?>


Thanks you guys
Malaysia > KL > TAR College
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jun 17, 2005
  • Posts: 515
  • Status: Offline

Post March 28th, 2006, 3:02 pm

Do you have that code both in your iframe source code, and also on your main page's source code?
  • hgun77
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jun 29, 2005
  • Posts: 158
  • Loc: Malaysia
  • Status: Offline

Post March 29th, 2006, 6:09 am

Nop, hiah... :(
so hard to find the problem, code only write once....
sad sad...
Malaysia > KL > TAR College
  • gisele
  • Expert
  • Expert
  • User avatar
  • Joined: Nov 11, 2004
  • Posts: 579
  • Loc: Nimes (France)
  • Status: Offline

Post March 29th, 2006, 6:32 am

hi,
what does "2 records for me" means?
If you refresh within 120 seconds you we'll have 2 entries in your table won't you? That's what "distinct" is in count function for.

Unless you mean 2 entries in both table? with the same timestamp?
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Joined: Apr 28, 2004
  • Posts: 1100
  • Loc: Atlanta, GA
  • Status: Offline

Post March 29th, 2006, 7:58 am

You are running two INSERTS, though on two different tables. I assume you mean its inserting two records in the same table each time. Which table is getting more than one INSERT?

Perhaps clearing the @mysql_query array would do the trick? Might be its holding the previous queries and running all the previous ones successively each time you call it).

.c
  • hgun77
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jun 29, 2005
  • Posts: 158
  • Loc: Malaysia
  • Status: Offline

Post March 29th, 2006, 3:31 pm

I FOUND THE CAUSE OF THIS PROBLEM.

But i don't know why and how to solve it.
First let me tell you guys how i troubleshoot it.

Before that i have to let you all know that my index.php i generate by dreamweaver 8 template.

Means index.php = DW8 templete + my HTML + my PHP


1) I copy that index.php in to test.php
2)I delete all the other my HTML content and only left DW8 templete + my PHP
- The result is same( insert twice each refresh)
3)I delete all the other DW8 templete language (example: <!-- InstanceBeginEditable name="left_navigator" -->) and only left my PHP Code now.
- GOT THE RESULT I WANT ( insert once each refresh)

This is what i discover after look on it and trobleshoot a few hours.

Why after i delete DREAMWEAVER TEMPLATE 8 language, then it will be ok ?
This is so "amazing"... I need someone who know help me, or explain me...

p/s: you can download my index.php at http://www.energy-tips.com/help_index.rar

Thanks.
Malaysia > KL > TAR College
  • hgun77
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jun 29, 2005
  • Posts: 158
  • Loc: Malaysia
  • Status: Offline

Post March 30th, 2006, 12:08 am

Problem solve. Sorry for my stupidness.
I include an external css file by this stupid line

Code: [ Download ] [ Select ]
<link rel="stylesheet" type="text/css" href="#" />
Malaysia > KL > TAR College
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Joined: Apr 28, 2004
  • Posts: 1100
  • Loc: Atlanta, GA
  • Status: Offline

Post March 30th, 2006, 5:32 am

hgun77 wrote:
Problem solve. Sorry for my stupidness.
I include an external css file by this stupid line

Code: [ Download ] [ Select ]
<link rel="stylesheet" type="text/css" href="#" />


Huh... interesting error. Makes sense though, (for anyone reading this topic that didn't pick up on this right away). The # sign in the links HREF element was causing the browser to request the current page (index.php#) as it was downloading it already.... sort of like putting a link to the page on the page itself, and clicking it as soon as the page loaded.

Its good you actually know HTML. If you were just another WYSIWYG monkey, you'd have probably never figured it out.

.c
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jun 17, 2005
  • Posts: 515
  • Status: Offline

Post March 30th, 2006, 4:11 pm

I should have downloaded the index file you provided. That # issue didn't even cross my mind, but it's definately the cause. Good job finding the error location.
  • hgun77
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jun 29, 2005
  • Posts: 158
  • Loc: Malaysia
  • Status: Offline

Post March 30th, 2006, 6:34 pm

Yes, hope every webpage designer and programmer can remember this error.
Malaysia > KL > TAR College

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 164 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.