PDA

View Full Version : Probably an easy Hit Counter error


dagaffer
09-15-2002, 03:46 PM
Ok, I'm hopeing this one will be short & sweet:
A very new asp person trying to make a very easy Hit Counter.

I'm using the code:
<%
Set DB = Server.CreateObject("ADODB.Connection")
sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("\totallys\db\DB.mdb") & ";" & _
"Persist Security Info=False"
DB.Open(sConnection)

SET RS=DB.EXECUTE("UPDATE fCOUNTER SET COUNT = COUNT +1")

SET DB=NOTHING

DB.Close
%>

& getting the error:
Microsoft JET Database Engine error '80040e14'

Syntax error in UPDATE statement.

/totallys/counter.asp, line 8


With fCOUNTER being the table name & COUNT being the columname, whats the problem?

dfrancis
09-15-2002, 04:17 PM
Try taking out "SET RS="

It should execute without trying to set a recordset.

dagaffer
09-16-2002, 08:01 PM
I took out the "Set RS=" & it comes up with:
Microsoft JET Database Engine error '80040e14'

Syntax error in UPDATE statement.

/totallys/counter.asp, line 8


Any more suggestions?

whammy
09-17-2002, 12:05 AM
Set DB = Server.CreateObject("ADODB.Connection")
sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("\totallys\db\DB.mdb") & ";" & _
"Persist Security Info=False"
DB.Open(sConnection)

SET RS = DB.EXECUTE("SELECT COUNT AS COUNT from fCOUNTER")
COUNT = RS("COUNT")

DB.EXECUTE("UPDATE fCOUNTER SET COUNT = " & COUNT +1 )

SET DB=NOTHING

DB.Close

Try that... I don't know if it will work though because I have a very painful toothache and it's making it hard to think straight

Mhtml
09-17-2002, 07:50 AM
If you are looking for really simple and really easy use a text file..
This one contains a cookies function to stop it from incrementing each time a user visits, but you could have it as session IP or even all three if you wanted..

[counter]
<%
Set fs = CreateObject("Scripting.FileSystemObject")
TxtFile=server.mappath("aspcount.txt")
on error resume next
Set a = fs.OpenTextFile(TxtFile)
Count = Clng(a.ReadLine)
Count = Count + 1
a.close
if error then
hits = 1
end if

if Request.Cookies("counted") = "yellow" then
Set a = fs.CreateTextFile(TxtFile,True)
a.WriteLine(Count)
a.Close
end if %>
<%
Response.Cookies("counted") = "purple"
%>

You are visitor <% =Count %>
[/code]

dagaffer
09-18-2002, 07:54 PM
Well Whammys idea just comes up with the same error & MHtml's idea came up with the error "Permission Denied".
I have the bad feeling that I should've told you this before as it probably makes a difference that Im using the free brinkster & I know it has some limitations to it.... Could this be the problem coz this is just getting annoying now:mad:

But anyway, thanx for the help so far, keep it comeing!:thumbsup:

whammy
09-18-2002, 11:02 PM
That would explain the "Permission Denied" error. I'd make sure you read over what you get with Brinkster's free service.

dagaffer
09-19-2002, 08:22 PM
Right, after the 1st failures, I found a script which was made for Brinkster which goes like this:
<%
If session("hitcount") = "" Then '<!-- Has the count been updated already? If NO then proceed -->
Const ForReading = 1, ForWriting = 2, ForAppending = 8 '<!-- define constants for FileSystemObject -->
Dim hitcount, fso, f, filen '<!-- define variables -->
filen = Server.MapPath("/totallys/db/hitcount.txt") '<!-- set the path to the hitcount.txt file -->
Set fso = CreateObject("Scripting.FileSystemObject") '<!-- create an instance of the FSO object -->
Set f = fso.OpenTextFile(filen, ForReading) '<!-- Open the textfile so we can read from it -->
If f.AtEndOfStream Then '<!-- hitcount.txt did not exist or contains no data -->
hitcount = "0" '<!-- set a value for hitcount -->
Else
hitcount = f.readall '<!-- read ALL the data from that file (should just be one line representing a count value) -->
End if '<!-- f.AtEndOfStream -->
f.close '<!-- Close the file -->
'<!-- You had the End if line here as well -->
If hitcount = "0" Then '<!-- check the count value, is it 0? -->
hitcount = 1 '<!-- yes it is: make it = 1 -->
Else
hitcount = Cint(hitcount) + 1 '<!-- no it is not: convert string value to Integer and then add 1 -->
End if '<!-- hitcount = "0" -->
hitcount = Cstr(hitcount) '<!-- make the hitcount a string again -->
Set f = fso.OpenTextFile(filen, ForWriting) '<!-- open the text file so we can write to it. -->
f.writeline(hitcount) '<!-- write the count value into the file: this overwrites the existing data -->
f.close '<!-- close the file -->
Set f = nothing '<!-- these two lines set the variables to nothing to free up server memory -->
Set fso = nothing

Select Case Len(hitcount) '<!-- how long is the hitcount? (i.e. how many digits - we want to standardise on 6) -->

Case 1 hitcount = "00000" & hitcount '<!-- count = 1 to 9; make it 000001 to 000009 -->
Case 2 hitcount = "0000" & hitcount '<!-- count = 10 to 99; make it 000010 to 000099 -->
Case 3 hitcount = "000" & hitcount '<!-- count = 100 to 999; make it 000100 to 000999 -->
Case 4 hitcount = "00" & hitcount '<!-- count = 1000 to 9999; make it 001000 to 009999 -->
Case 5 hitcount = "0" & hitcount '<!-- count = 10000 to 99999; make it 010000 to 099999 -->
'<!-- if the count is already 100000 to 999999 then we do not need to add a leading 0 -->
End Select
session("hitcount") = hitcount '<!-- put this value into a Session variable: see first line -->
End if '<-- session("hitcount") = ""
%>


Firstly, is there something in that script that would cause the counter to go away if the same person came back to that page pretty quicky (or refreshed that page...)?

Secondly, is there some way of getting out the Cases so that it is just displayed as 1...2 & not 000001...?

(I think at least 1 of those questions is fairly easy) Thanx for the help so far & keep it comeing!:thumbsup:

whammy
09-19-2002, 08:34 PM
To answer your first question: Yes, a session variable is being set once the counter is updated, so it isn't updated when refreshed.


In answer to your second question, take out the part that says:

Select Case Len(hitcount)

and the part of the script associated with it. Besides, if you DID want to make sure you used leading zeroes, I can show you a MUCH MUCH easier way to do that which will turn this:


Select Case Len(hitcount) '<!-- how long is the hitcount? (i.e. how many digits - we want to standardise on 6) -->

Case 1 hitcount = "00000" & hitcount '<!-- count = 1 to 9; make it 000001 to 000009 -->
Case 2 hitcount = "0000" & hitcount '<!-- count = 10 to 99; make it 000010 to 000099 -->
Case 3 hitcount = "000" & hitcount '<!-- count = 100 to 999; make it 000100 to 000999 -->
Case 4 hitcount = "00" & hitcount '<!-- count = 1000 to 9999; make it 001000 to 009999 -->
Case 5 hitcount = "0" & hitcount '<!-- count = 10000 to 99999; make it 010000 to 099999 -->
'<!-- if the count is already 100000 to 999999 then we do not need to add a leading 0 -->
End Select


Into this:


hitcount = Right("000000" & hitcount,6)


A little shorter, eh? :)

Not to mention I'm still scratching my head trying to figure out why there would be HTML comments within an asp comment... if anything it makes the comments harder to read!

dagaffer
09-19-2002, 08:46 PM
Lol, i see wat you mean:D .
I'm not good at ASP (or really any language) so i didn't know wat anything meant...
The HTML comments were there when i got it... I just left it there.

Is there some way I can get rid of the session variavle thing (I get little hits as it is so I want it to make it look like a lot of people have been without officially cheating (eg. starting the counter @ a couple of thousand...)

The "Easy" error I think is just about over:thumbsup:

whammy
09-19-2002, 09:59 PM
Yeah just take out the

If session("hitcount") = "" Then '<!-- Has the count been updated already? If NO then proceed -->

part, and the corresponding "Else" and "End If" statements.

That's put in there so noone can "artificially" inflate the hit counter by refreshing the page.

dagaffer
09-19-2002, 10:04 PM
I hope thisl b the last post 2 this!
I hope thatl do the trick, Thanx 4 all of ur help!
Gaffer.

whammy
09-19-2002, 10:37 PM
If that doesn't help, try something like this (although I just tested this as a local .vbs file since I don't have .asp installed on this machine):


Dim fs, counter, thefile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
thefile = "test.txt" 'Webpage would use Server.MapPath("test.txt")
Set fs = CreateObject("Scripting.FileSystemObject") 'Webpage would use Server.CreateObject
If fs.FileExists(thefile) = True Then
Set f = fs.OpenTextFile(thefile, ForReading)
counter = f.ReadLine + 1
Set f = fs.OpenTextFile(thefile, ForWriting)
f.WriteLine(counter)
Else
counter = 1
fs.CreateTextFile(thefile)
Set f = fs.OpenTextFile(thefile, ForWriting)
f.WriteLine(counter)
End If
Set fs = Nothing
MsgBox(Right("000000" & counter,6))

Mhtml
09-20-2002, 08:32 AM
I forgot all about this thread, sorry.

Brinkster free sites are set up so that the only folder with write permissions is the db folder.

So to make my example work all you had to do was put a textfile in the db folder and then change the mappath variable.

by the way morgoth just started a hosting service thing, I guess not as stable as brinkster was when it was young but more than it is now...

http://24.226.62.28

Try that, that is if you get excepted...

Mhtml
09-20-2002, 08:37 AM
I'm working on a stats script if you want I could notify you when it is finished and you can download it from my site.

whammy
09-20-2002, 01:02 PM
LOL... so am I... like unique hits per page, browser type, ip address, etc.

Mhtml
09-20-2002, 01:17 PM
sounds almost exactly like what I'm doing...
Plus I'm doing a link thing which you just replied to whammy..

BTW how are you going to do unique hits? Cookies? IP? Just wondering because you can't exactly get a absoloute result via those because some peeps may have server asgnn IP and may delete their cookies every once in a while...

whammy
09-20-2002, 03:23 PM
Well, I just go by unique hits per ip address. You can't account for everything.

P.S. I made a counter script that should work ok anywhere, but on brinkster you'll need to change the path to the counter.txt file to reside in the database folder:


<%
Dim fs, f, counter, thefile
thefile = Server.MapPath("counter.txt")
Set fs = Server.CreateObject("Scripting.FileSystemObject")
If fs.FileExists(thefile) = True Then
ReadCounter()
If session("counted") <> "yes" Then
WriteCounter()
End If
Else
fs.CreateTextFile(thefile)
WriteCounter()
End If
Set fs = Nothing

Sub ReadCounter()
Set f = fs.OpenTextFile(thefile, 1)
counter = f.ReadLine
End Sub

Sub WriteCounter()
Set f = fs.OpenTextFile(thefile, 2)
counter = counter + 1
f.WriteLine(counter)
session("counted") = "yes"
End Sub
%>

<% = counter %>

Mhtml
09-21-2002, 06:02 AM
that's neat , it creates the file if it doesn't exist..interesting...

whammy
09-21-2002, 04:44 PM
Yeah, thanks to a little studying on www.w3schools.com and some messing around. ;)