Can somebody explain how this asp code works?

蓝咒 提交于 2019-12-13 08:24:39

问题


I wanted to echo some data from a table in my database and found this code:

set rs = Server.CreateObject("ADODB.recordset")

rs.Open "Select name From users", conn

do until rs.EOF
    for each x in rs.Fields
        Response.Write(x.value)
    next
    rs.MoveNext
loop

rs.close

I tested it and and it worked but I have no clue what all that syntax means and there is no explanation provided with the code. Can someone with experience help me out?


回答1:


rs is a recordset, which means a result of a database query.

The do until ... loop iterates (with the help of movenext) through all the rows found in the recordset (i.e. table users).

On all rows found the for each ... next loops through all fields found in the single row, which in this case is only the column name.




回答2:


See comments added to the code below

' Create a recordset. This is an object that can hold the results of a query.
set rs = Server.CreateObject("ADODB.recordset")

' Open the recordset for the query specified, using the connection "conn"
rs.Open "Select name From users", conn

' Loop over the results of the query until End Of File (EOF)
'   i.e. move one at a time through the records until there are no more left
do until rs.EOF
    ' For each field in this record
    for each x in rs.Fields

        ' Write out its value to screen
        Response.Write(x.value)

    ' Move to the next field
    next

    ' Move to the next record
    rs.MoveNext

' Continue to loop until EOF
loop

' Close the recordset
rs.close


来源:https://stackoverflow.com/questions/21705266/can-somebody-explain-how-this-asp-code-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!