Home

FETCHASSOC

FETCHASSOC is a term used to describe a fetch mode in database access libraries that returns a single row as an associative data structure. In this mode, each field is addressed by its column name, so the result is a mapping from column names (strings) to their corresponding values. This contrasts with numeric fetch modes that use column positions.

In practice, the concept appears in several languages and APIs under slightly different naming. The common

Usage typically involves selecting fetch mode when executing a query or when retrieving results, for example

Advantages of FETCHASSOC include improved readability and resilience to changes in column order, since code references

See also: FETCH_NUM, FETCH_BOTH, and database API fetch modes.

variant
is
FETCH_ASSOC,
often
implemented
as
a
constant
or
option
such
as
PDO::FETCH_ASSOC
in
PHP
or
a
dedicated
function
that
returns
an
associative
array.
Some
libraries
also
provide
a
direct
function
like
fetch_assoc
that
yields
the
same
result.
Other
modes
may
exist,
such
as
FETCH_NUM
(numeric
indices
only)
or
FETCH_BOTH
(both
numeric
and
associative
keys).
by
calling
a
fetch
method
with
the
associative
option
or
by
using
a
function
that
implicitly
returns
an
associative
mapping.
If
a
library
supports
default
fetch
modes,
setting
it
to
FETCHASSOC
ensures
that
all
retrieved
rows
are
accessed
by
column
name.
fields
by
name
rather
than
position.
Potential
drawbacks
include
higher
memory
usage
compared
to
numeric
fetch
modes
and
the
need
to
reference
exact
column
names,
which
may
require
handling
of
case
sensitivity
or
aliasing.