Help with ASP and MYSQL

Discussion in 'Software' started by aceface, Dec 12, 2006.

  1. aceface

    aceface Private E-2

    Hi guys,
    I'm making a link tracker using ASP and reading/entering data into a MYSQL database.

    I think i'm almost there but for a few niggly errors with syntax and stuff

    Error:

    Microsoft VBScript runtime error '800a01f4'

    Variable is undefined: 'adOpenDynamic'

    /counter/LinkTrackerx.asp, line 81


    Line 81 is the following:
    adOpenDynamic, adLockPessimistic, adCmdText

    And i'm not sure why it is doing this? As I've included the adovbs file too

    Any help would be appreciated!
    Matt

    <%@ Language=VBScript %>
    <%
    Option Explicit ' Never code without it!
    Response.Buffer = True ' Make sure we can redirect later

    ' The constants I use in this script...
    ' pulled directly from adovbs.inc

    ' This needs to be ' for SQL Server and # for Access
    Const DATE_DELIMITER = "'"

    Dim strName ' Friendlyname of the redirect
    Dim cnnLinkTracker ' ADO objects
    Dim rsLinkTracker
    Dim strSQL ' SQL command building area
    Dim iRedirectId ' id of the redirect for logging process


    ' Get name of location to go to
    ' I simply call this name so to call the script it would look
    ' something like this:
    ' http://server/linktracker.asp?name=asp101
    ' where asp101 is the friendly name from the database
    strName = Request.QueryString("name")

    ' Make it DB friendly just in case a ' got in somehow
    strName = Replace(strName, "'", "''")

    ' Your connection string goes here!
    set cnnLinkTracker = Server.CreateObject("ADODB.Connection")
    cnnLinkTracker.Open="Driver={MySQL ODBC 3.51 Driver};Server=mysql8.streamline.net;Port=3306;Database=xxxxxxx;User=xxxxxxx;Password=xxxxxx;Option=3;"

    ' Create a recordset
    Set rsLinkTracker = Server.CreateObject("ADODB.Recordset")

    ' Get the record we're looking for by passing it the name
    ' we got from the QueryString. I'm prebuilding the SQL command
    ' to make it easier to debug if we need to.
    strSQL = "SELECT id, location FROM tblLinkTracker " & _
    "WHERE name='" & strName & "';"

    ' Quick and dirty debugging when something goes wrong.

    ' Send the command to the database to get the appropriate records
    rsLinkTracker.Open strSQL, cnnLinkTracker, _
    adOpenForwardOnly, adLockReadOnly, adCmdText

    ' If we got back any results then we know where to send them
    ' o/w I just send them to our home page for lack of a better
    ' place to send them.
    If Not rsLinkTracker.EOF Then
    ' Get redirect Id ' from recordset
    iRedirectId = rsLinkTracker.Fields("id").Value

    ' Get location to send the user to
    strName = rsLinkTracker.Fields("location").Value

    ' We've got all the info we need so close our recordset
    rsLinkTracker.Close

    ' I now recycle the recordset for the logging process.
    ' Lots of people would argue with me about this, but I
    ' know I'm doing it and this is my code so if you don't
    ' like it feel free to change it, but I'm not going to! ;)

    ' Start logging process

    ' Build out SQL String ahead of time.
    ' This should get us the record containing the information
    ' for the selected link for today's date if one exists.
    strSQL = "SELECT link_id, hit_date, hit_count " & _
    "FROM tblLinkTrackerLog " & _
    "WHERE link_id = " & iRedirectId & " " & _
    "AND hit_date = " & DATE_DELIMITER & Date() & _
    DATE_DELIMITER

    ' Standard debugging step when something goes wrong!

    ' Send the command.
    rsLinkTracker.Open strSQL, cnnLinkTracker
    adOpenDynamic, adLockPessimistic, adCmdText

    ' If it's EOF then it's the first hit of the day and we need
    ' to add a new record o/w we simply update the existing hit
    ' count of the record by adding one to it.
    If rsLinkTracker.EOF Then
    rsLinkTracker.AddNew

    rsLinkTracker.Fields("link_id").Value = iRedirectId
    rsLinkTracker.Fields("hit_date").Value = Date()
    rsLinkTracker.Fields("hit_count").Value = 1
    Else
    rsLinkTracker.Fields("hit_count").Value = _
    rsLinkTracker.Fields("hit_count").Value + 1
    End If

    ' Save changes to the data
    rsLinkTracker.Update
    Else
    ' If no match send em to our home page
    strName = "/"
    End If

    ' Close our recordset object
    rsLinkTracker.Close
    Set rsLinkTracker = Nothing

    ' Kill our connection
    cnnLinkTracker.Close
    Set cnnLinkTracker = Nothing

    ' Send them on their merry way using the location we got
    ' from the database!
    Response.Redirect strName
    %><!-- #include file="adovbs.inc" -->
     
  2. SoloTraveller

    SoloTraveller Private First Class

    Can't really tell from the code posted here, but adOpenDynamic is a constant value that's part of the Open method. It appears that you have it on a separate line, which is incorrect. Get rid of the carriage return so the optional parameter list is part of the Open method.

    A quick search of MSDN should yield the value for that constant; I don't recall off the top of my head what it is.
     
  3. aceface

    aceface Private E-2

    thank you solo traveller :)

    have managed to fix that portion of the code, I can now get it all working and reading table entries.

    Only problem now is the error returning Duplicate values in my table, the link_id is affected - it seems it will only write to the table once, and not update the hit count.
    So the script will tell me ERROR when it tries to enter another hit for the same link_id

    Matt
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds