Need help connecting to a MySQL database with ASP.

by MarkR
10 replies
Background - I'm moving a big ASP site that uses MySQL to a new server. I've already exported/imported the database successfully. Now I can't get the ASP code to connect to the new database on the new server.

The code that the new hosting company said to use looks like this:

================================
<% Dim ConnectionString ConnectionString="DRIVER={MySQL ODBC 3.51 Driver}; SERVER=acct.ipowermysql.com; PORT=3306;" &_ "DATABASE=dbname; USER=username; PASSWORD=*pass*; OPTION=3;" %>
================================


The original code from the site I'm moving looks like this:

================================
'local
Const SITE_URL = "http://url.com/"
Const ROOT_FOLDER = "/"
Const BASE_URL = "/"
Const MAIL_SERVER = ""
Const VERISIGN_URL = ""
Const SITE_SSL = "http://mmd-3/"

Set Conn = Server.CreateObject("ADODB.Connection")
'Conn.mode=3
'Conn.open "DSN=dsn1"
'Conn.open "DSN=dsn2"

'Conn.Open "Driver={SQL Server};Server=999.999.999.999;Database=dbname;Uid =user;Pwd=pass;"
'Conn.Open "Driver={SQLServer};Server=999.999.999.999;Databas e=dbname;Trusted_Connection=yes;"
'Conn.Open "Driver={SQL Server};Server=999.999.999.999;Uid=user;Pwd=pass;D atabase=dbname;"
'Conn.Open "Driver={mySQL};Server=mysql.serverurl.com;Databas e=dbname1;UID=user1;PASSWORD=pass1"
Conn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=h41mysql1.serverurl.net;PORT=3306;D ATABASE=dbname;USER=user;PASSWORD='pass';OPTION=0; "

REM:///////////

'-------------------------------------------
'Open the DB Connection
'-------------------------------------------
function fnDBOpen()

Set Conn = Server.CreateObject("ADODB.Connection")
'Conn.open "DSN=dsn1"
'Conn.open "DSN=dsn2"
'Conn.Open "Driver={SQL Server};Server=999.999.999.999;Uid=user;Pwd=pass;D atabase=dbname;"
Conn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=h41mysql1.serverurl.net;PORT=3306;D ATABASE=dbname;USER=user;PASSWORD='pass';OPTION=0; "

end function
================================

I'm an HTML guy, not an ASP guy, and never used a MySQL database before either. So, I'm in over my head on this one. If it helps, I'll share the ASP code that I edited to connect to the database (that doesn't work).

Any help would be most appreciated!

Mark
#asp #connecting #database #mysql
  • Profile picture of the author KirkMcD
    It looks like you should change this:
    SERVER=h41mysql1.serverurl.net;
    to
    SERVER=acct.ipowermysql.com;

    Also, the single quote in front of a line means it has been commented out and can be ignored.
    {{ DiscussionBoard.errors[1479505].message }}
    • Profile picture of the author MarkR
      Kirk,

      Thanks, I did make that change, and it didn't work. I'm getting an error as follows:


      Microsoft VBScript compilation error '800a0408'

      Invalid character

      /functions/functions.asp, line 35

      Conn.Open "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=acct.ipowermysql.com; PORT=3306;" &_ "DATABASE=dbname; USER=username; PASSWORD=*password*; OPTION=3;"----------------------------------------------------------------------------------------^

      It doesn't like the third set of double quotes before DATABASE=dbname, which I assume are there to connect the two halves of the command, as ASP doesn't like it when you break this command across lines.Mark
      {{ DiscussionBoard.errors[1480052].message }}
  • Profile picture of the author HomeComputerGames
    It's been awhile since I worked with ASP but perhaps it's the &_ ( I believe this is to include the next line.)
    Perhaps try this on one line:
    Conn.Open "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=acct.ipowermysql.com; PORT=3306; DATABASE=dbname; USER=username; PASSWORD=*password*;OPTION=3;"
    Signature

    yes, I am....

    {{ DiscussionBoard.errors[1480115].message }}
  • Profile picture of the author VisualWebEffects
    it has been a while since i touched asp but there is one thing that is the same in asp/asp.net/and .net winforms.

    &_

    should have a space between them. so: & _

    if that does not work, put it as one single line instead.
    Signature
    VisualWebEffects- Web Application Development, PC Software Development and Identity Design services
    {{ DiscussionBoard.errors[1481014].message }}
  • Profile picture of the author Mr. Enthusiastic
    Hi Mark,

    What you have there is some darn klutzy ASP code.

    Microsoft moved from ASP to ASP.Net ten years ago. Is there a good reason to keep this thing in old-school ASP instead of moving into the current century?

    As Kirk mentioned, a single quote at the start of a line indicates comments. The developer should have entirely removed, rather than commenting out, the junk that was no longer needed, such as the alternative Conn.Open lines for different servers.

    Conn.Open takes a string. I like to build the string first, then pass it to Conn.Open. And to build the string, I like to refer to constants that I group together so the code is easy to maintain:

    ' database access constants
    const dbServer = "acct.ipowermysql.com"
    const dbPort = "3306"
    const dbUser = "userNameGoesHere"
    const dbPass = "passWordGoesHere"
    const dbDatabase = "dataBaseNameGoesHere"
    const dbDriver="{MySQL ODBC 3.51 Driver}"
    const dbAppend = "OPTION=3";

    dbConn = "DRIVER=" & dbDriver & ";"
    dbConn = dbConn & "SERVER=" & dbServer & ";"
    dbConn = dbConn & "PORT=" & dbPort & ";"
    dbConn = dbConn & "DATABASE=" & dbDatabase & ";"
    dbConn = dbConn & "USER=" & dbUser & ";"
    dbConn = dbConn & "PASSWORD=" & dbPass & ";"
    dbConn = dbConn & dbAppend
    ' should really get saved as a global variable in the Application object

    conn.Open dbConn

    The next problem is why the database connection is opened two different places. There should be exactly one function in the code that encapsulates opening the database, and it should be in a library that's separate from both your GUI code and your business rules.

    But the real problem is why to use the obsolete and very, very limited ASP environment rather than moving on to ASP.Net, which makes this kind of configuration so much easier (if people know what they're doing).

    Chris
    {{ DiscussionBoard.errors[1491798].message }}
    • Profile picture of the author MarkR
      Wow, after a ton of trial and error, I finally got this to work. Turns out that the & _ (with or without a space) wasn't needed or at least didn't work on my host. I hate dealing with ancient code.

      Thanks for all your help. I love this place!
      {{ DiscussionBoard.errors[1512673].message }}
      • Profile picture of the author Mr. Enthusiastic
        Originally Posted by MarkR View Post

        I hate dealing with ancient code.
        Nicely done. We should call you "The Archeologist."
        {{ DiscussionBoard.errors[1513734].message }}
      • Profile picture of the author HomeComputerGames
        Originally Posted by MarkR View Post

        Wow, after a ton of trial and error, I finally got this to work. Turns out that the & _ (with or without a space) wasn't needed or at least didn't work on my host. I hate dealing with ancient code.

        Thanks for all your help. I love this place!
        Ahhh, so I was right

        Glad you got it working
        Signature

        yes, I am....

        {{ DiscussionBoard.errors[1516486].message }}
  • Profile picture of the author buncaila
    i think no direct connection is available for asp/mysql like php/mysql. but you can connect through ODBC . I have dont that few years ago. But why ASP yet? use php or .net

    anyway thanks
    {{ DiscussionBoard.errors[1517388].message }}

Trending Topics