Connect to a MySQL database

In this example we will show you how to connect to a MySQL database . Create a database like this.

CREATE DATABASE example;
USE example;

CREATE TABLE example(url VARCHAR(40) , description TEXT);

INSERT INTO example VALUES('http://www.google.com' , ' The best search engine');
INSERT INTO example VALUES('http://www.beginnersphp.co.uk' , 'top notch php site');
INSERT INTO example VALUES('http://www.myscripting.com','Scripting resources galore here');

Script :

<%
Dim adOpenForwardOnly , adLockReadOnly , adCmdTable
adOpenForwardOnly = 0
adLockReadOnly =1
adCmdTable =2
Dim objConn , objRS
'create our objects
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.Recordset")
'connect to the MySQL example database
objConn.Open "driver={MySQL};server=localhost;uid=iainhendry;pwd=iain06;database=example"
'open the example table
objRS.Open "example" , objConn , adOpenForwardOnly , adLockReadOnly , adCmdTable
display all data
While Not objRS.EOF
Response.Write objRS.Fields("URL") & "&nbsp"
Response.Write objRS.Fields("description") & "&nbsp"
Response.Write "<p>"
objRS.MoveNext
Wend
'close recordset
objRS.Close
'destroy all objects
Set objRS = Nothing
Set onjConn = Nothing
%>

Sponsors