MySQL DB connection

Connects to MySQL database from Classic ASP
All DB variables need to be already defined
DBServer, DBName, DBUser, DBPass
Content below is used to be in Inc_Conn.asp file and included inside tool ASP files

Dim Connection
Dim ConnString
' define the connection string, specify database driver
ConnString = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=" & DBServer & "; DATABASE=" & DBName & _
 ";UID=" & DBUser & ";PASSWORD=" & DBPass & "; OPTION=3"
Set Connection = Server.CreateObject("ADODB.Connection")
Connection.Open ConnString
connection.execute "SET NAMES cp1256"
connection.execute "set character set cp1256"

' Another example \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Dim Connection, ConnectionString, Recordset, SQL
SQL       = "SELECT * FROM TABLE_NAME" ' declare the SQL statement that will query the database
ConnString     = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=Your_Mysql_DB; " &_
 "UID=mysql_username;PASSWORD=mysql_password; OPTION=3"
 ' define the connection string, specify database driver
' create an instance of the ADO connection and recordset objects
Set Connection    = Server.CreateObject("ADODB.Connection")
Set Recordset    = Server.CreateObject("ADODB.Recordset")
Connection.Open ConnString ' Open the connection to the database
Recordset.Open SQL,Connection ' Open the recordset object executing the SQL statement and return records
If Recordset.EOF Then ' first of all determine whether there are any records
 Response.Write("No records returned.")
 ' if there are records then loop through the fields
 Do While NOT Recordset.Eof
  Response.write Recordset("FIRST_FIELD_NAME")
  Response.write Recordset("SECOND_FIELD_NAME")
  Response.write Recordset("THIRD_FIELD_NAME")
  Response.write "
End If
Recordset.Close ' close the connection and recordset objects freeing up resources
Set Recordset=nothing
Set Connection=nothing

Views 2406 Downloads 706

'DBServer', 'DBName', 'DBUser and DBPass variables already defined', 'Database', 'DB', 'MySQL', 'connection'

Database Classic ASP