Use this example to see the core idea, then work through the complete steps below. Code snippets are Classic ASP / VBScript or SQL Server T-SQL as labelled, not PHP.
VBScript example
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "Provider=MSOLEDBSQL19;Data Source=sql01.example.com;" & _
"Initial Catalog=appdb;Integrated Security=SSPI;"
' Execute commands, then close when finished.
cn.Close
Set cn = NothingReplace the example server and database names; the installed OLE DB driver, Windows identity, encryption, and trusted certificate must be configured. Keep credentials out of public files.
Keep the SQL Server connection string in one server-side include so database pages share a single configuration. The example uses Windows integrated authentication, which avoids embedding a database password in the ASP source when IIS/SQL Server identities are configured appropriately. If SQL authentication is required, keep the secret in protected server configuration rather than client-visible code or downloads.
<!-- #include virtual="/conn.asp" -->
<%
Dim aConnectionString
aConnectionString = "Provider=MSOLEDBSQL19;" & _
"Data Source=sql01.example.com;" & _
"Initial Catalog=appdb;" & _
"Integrated Security=SSPI;"
%>
Microsoft recommends the current MSOLEDBSQL driver generation for SQL Server. Driver 19 uses stronger encryption defaults, so production environments should be configured with a trusted server certificate and connection settings appropriate to the installed driver.
<%@ Language="VBScript" %>
<% Option Explicit %>
<!-- #include virtual="/adovbs.inc" -->
<!-- #include virtual="/conn.asp" -->
<%
Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.Mode = adModeRead
conn.ConnectionString = aConnectionString
conn.Open
' Run database work here.
conn.Close
Set conn = Nothing
%>
Microsoft OLE DB Driver for SQL Server
Connection attempt failed because the server, credentials, network path, encryption or certificate configuration could not be validated.
A connection failure is not proof that SQL Server is absent. Check the server/instance name, TCP/network access, credentials, database permissions, installed OLE DB driver, encryption settings and certificate trust.
Use an IIS-protected server-side include for the connection string. Do not put database secrets in browser HTML or a downloadable ZIP. Production TLS certificate validation should not be disabled just to suppress connection errors.
Examples using names such as adCmdText and adVarWChar require the ADO constants to be declared, commonly with adovbs.inc. Some concise examples use the documented numeric values instead.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.