How to use MySQL in ASP

Source: Internet
Author: User
Tags connect mysql in ole query table name mysql database
Using A MySQL Databases
by Ben O ' Neill


Databases are the best way to keep your Web site up-to-date and dynamic. Databases are used
Thousands of web sites. They are used for storing news and general information. Web sites like the ASP
Index (www.aspin.com) are run on large databases. Databases make a Web site easy to update and once
Have the base script, to add, remove and modify things in a database are very easy.



To start your need to know how to connect to a database. ASP can connect to virtually any type, from
Microsoft Access to SQL. In this example I'll be using MySQL with OLE DB to connect to it.



MySQL can is downloaded from the MySQL Web site (www.mysql.com). You'll also need the provider used to
Connect to it, also available from the MySQL Web site.



Are you asking what ' s OLE DB? I ' m used to ODBC and DSN. OLE DB is faster and more stable. It ' s almost
Exactly the same.



We need to connect to the database, because it's a MySQL database you also need to supply a database
Name. (in MySQL, can have mulitple databases on the same SQL Server.)



<%

strconnection = "Driver={mysql};server=localhost;uid=benoneill;pwd=mypassword;database=databasename"



Set adodataconn = Server.CreateObject ("ADODB. Connection ")

Adodataconn.open strconnection

%>



And now we ' ve connected. Let's pretend we ' ve got a big list of lots and lots of emails addresses, here ' s
The contents of our we database, it allows me to show your IT works better.



Table Name:emailadds



name牋牋牋牋牋牋牋?牋牋牋牋牋?emailadd

------------------------------------------------------

Ben牋牋牋牋牋牋牋牋?牋牋牋牋牋?sheepcow@planetunreal.com

Fred牋牋牋牋牋牋牋牋 牋牋牋牋牋?freddy@thebigisp.com

Ben Harding 牋 牋 牋 牋 牋-牋? 牋

Dave geralding 牋 牋 牋 牋 牋? daveg@mymail.com



Now we have the "database" open let's run a query to list and output all the names and email address in a
Nice easy to view table.



<%

? strquery = "SELECT * FROM Emailadds"



? Set rsemaildata = Adodataconn.execute (strquery)



? If not Rsemaildata.bof Then

%>



<TABLE>



?/span><tr>

牋 <TD<b>Name</b></TD>

牋 <td><b>email address</b></td>

?/span></tr>



<%

? Do as not rsemaildata.eof

%>



?/span><tr>

牋 <td><%=rsemaildata ("name"). Value%></td>

牋 <td><%=rsemaildata ("Emailadd"). Value%></td>

?/span></tr>



<%

牋? rsemaildata.movenext

? Loop



%>



</TABLE>



<%

? Else



牋? Response.Write ("Sorry, no email addresses found.")



? End If

%>





There we go. If no records are found then it says "Sorry, no email addresses found".



That's simple enough, it tells the "database to get" (SELECT) all the records and all of the fields from
The table named Emailadds.



How about we do it only show people with the name "Ben" somewhere in their name and simple change the query
To this:



SELECT * from Emailadds WHERE name like '%ben% '


This query would return to only 2 records, Ben and Ben Harding.



It ' s important you to use single quotes ('), because double quotes won ' t work. can also be very selective
and do:



SELECT * from Emailadds WHERE name= ' Ben '


This query would only return Ben, not Fred or Ben Harding, or Dave geralding.



After using databases your should always clear up. Close the database and the record set, and set them to
The memory used by them is regained, does this by writing this:



<%

? rsemaildata.close

? adodataconn.close



? Set Adodataconn = Nothing

? Set Rsemaildata = Nothing

%>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.