The article introduces in detail how to connect to the MySQL database in ASP and find out the data and output it. It also introduces the solution to the core code of connecting to MySQL and the Chinese garbled code. If you want ASP to be able to connect to the MySQL database, you need to install the MySQL ODBC 3.51 driver.
Let’s look at the following code first
Copy the code code 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 connects to the server such as 127.0.0.1
DATABASE The database shops to select
USER server login user name
PASSWORD Server login password
Okay, let’s look at an example
Copy the code code as follows:
<%
'Test reading the contents 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.execute(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 did not set the database encoding above. If there are Chinese garbled characters, you can try it.
'Set client character encoding
Copy the code code as follows:
conn.execute(set names ' & myChareSet & ')
to solve
If you want to use a port other than 3306, we need to install the ODBC database driver for Mysql. After installation
Copy the code code 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.