Detailed implementation code for connecting asp to mysql database

Source: Internet
Author: User

To connect asp to a mysql database, you need to install the MySQL ODBC 3.51 driver http://www.jb51.net/softs/19910.html.
Let's take a look at the following code.

Copy codeThe Code is as follows: set conn = server. createobject ("adodb. connection ")
Conn. Open "DRIVER = {MySQL ODBC 3.51 Driver}; SERVER = 127.0.0.1; DATABASE = Shops; USER = root; PASSWORD = xxx ;"

The above code is the core part.

SERVER connection SERVER, such as 127.0.0.1
Shops of the DATABASE to be selected
USER Server login Username
PASSWORD server logon PASSWORD

Now let's take a look at the instance.Copy codeThe Code is as follows: <%
'Test the reading content of the MySql database
Strconnection = "driver = {mysql odbc 3.51 driver}; database = weste_net; server = localhost; uid = root; password ="
'No need to configure dsn
Set adodataconn = server. createobject ("adodb. connection ")
Adodataconn. open strconnection
Strquery = "select * from News"
Set rs = adodataconn.exe cute (strquery)
If not rs. bof then
%>
<Table>
<Tr>
<Td <B> serial number </B> </td>
<Td> <B> title </B> </td>
</Tr>
<%
Do while not rs. eof
%>
<Tr>
<Td> <% = rs ("News_id") %> </td>
<Td> <% = rs ("News_Title") %> </td>
</Tr>
<%
Rs. movenext
Loop
%>
</Table>
<%
Else
Response. write ("no data .")
End if
Rs. close
Adodataconn. close
Set adodataconn = nothing
Set rsemaildata = nothing
%>

Note
I didn't set the database encoding above. If there are Chinese garbled characters, try again.

'Set the client character encodingCopy codeThe Code is as follows: conn.exe cute ("set names '" & myChareSet &"'")

To solve
To use a port other than 3306, We need to install the Mysql ODBC database driver.

Copy codeThe Code is as follows: Conn. Open "DRIVER = {MySQL ODBC 3.51 Driver}; SERVER = 127.0.0.1; PORT = 3333; DATABASE = Shops; USER = root; PASSWORD = xxx ;"

There is no problem.

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.