Use this example to see the core idea, then work through the complete steps below. Code snippets are Classic ASP / VBScript or SQL Server T-SQL as labelled, not PHP.
VBScript example
If rs.EOF And rs.BOF Then
Response.Write "No matching records"
Else
Do While Not rs.EOF
Response.Write Server.HTMLEncode(CStr(rs("name"))) & "<br>"
rs.MoveNext
Loop
End IfAssumes an open rs Recordset with a name field. An empty Recordset has both BOF and EOF set to True.
An ADO Recordset keeps a current-row position while you browse query results. The cursor type used when the Recordset is opened determines which movement operations are available.
EOF becomes true after the current position moves beyond the last record. A forward-reading loop normally checks Do While Not rs.EOF before reading fields.
BOF indicates that the current position is before the first record. If a Recordset opens with no rows, both BOF and EOF are true and there is no current record.
MoveNext advances to the next row. Calling it when EOF is already true raises an error, so a normal loop stops when EOF becomes true.
Backward movement and direct first/last navigation require a Recordset/cursor that supports those operations. A forward-only Recordset is designed primarily for sequential MoveNext access.
RecordCount is cursor/provider dependent and can return -1 for a forward-only cursor. When you only need a count, a SQL COUNT(*) query is usually clearer and avoids opening a scrollable cursor just to count rows.
Use a forward-only Recordset for sequential display. The cursor usually does not support moving backward, and its RecordCount can be -1.
If you only need the number of records, use an aggregate query rather than moving the Recordset cursor.
SELECT COUNT(*) AS total_rows FROM emp_m;
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.