http://qs321.pair.com?node_id=276232


in reply to Going to first record on a record set in DBI

Using DBI, you access the recordset in two ways:

A word of caution, though. If you are referring to the first physical record in a table, then be aware that there is no such a thing in a relational database. You can have a record where a specific column (e.g. the primary key) is "1", but it does not mean that this is the "first" record, whatever you mean by that.

The data set that you are "navigating" with the DBI is the result of a query, which can be influenced by WHERE and ORDER BY clauses. This data set is not guaranteed to have any relation whatsoever with the physical order of the records in your tables. There are some DBMS that implement a "record number" or similar attribute, but it is not standard, not portable, not relational.

See also the discussion following this node for some practical examples.

And, I almost forgot. You are using a "fetchrow" record. It does not exist. It is not a documented method. The methods at your disposal are "fetchrow_array," "fetchrow_arrayref," or "fetchrow_hashref."

Update
Thanks to cfreak, who points out that "fetchrow" exists and it is an alias for "fetchrow_array", even though it is not documented.
I acknowledge it, but I still think it should be better to use one of the named methods I listed above, for two reasons:

See Reading from a database in our Tutorials for more information.

_ _ _ _ (_|| | |(_|>< _|

Replies are listed 'Best First'.
Re: Going to first record on a record set in DBI
by Abigail-II (Bishop) on Jul 21, 2003 at 12:22 UTC
    Indeed. In a relational way of thinking, it only make sense to talk about the "first N rows", if you have a query with an ORDER BT clause. If you want the "first row" given some ordering, you can often do that with a WHERE clause. Say:
    SELECT name, gender FROM people ORDER BY age LIMIT 1

    can also be written as

    SELECT name, gender FROM people WHERE age = MIN (age)

    That is, you ask for the record with minimum age.

    Abigail

Re: Re: Going to first record on a record set in DBI
by cfreak (Chaplain) on Jul 21, 2003 at 13:28 UTC

    And, I almost forgot. You are using a "fetchrow" record. It does not exist. The methods at your disposal are "fetchrow_array," "fetchrow_arrayref," or "fetchrow_hashref."

    Not true. The fetchrow() method does indeed exist and works quite well. It returns an array of the current row like fetchrow_array()

    Granted, the DBI documentation should be updated to reflect that.

    Lobster Aliens Are attacking the world!